duanpanzhu2910 2014-04-29 12:04
浏览 29

php loggedin()提供错误的标题

I'm new to php, so I follow a tutorial from WellBro. I'm making a registration and login. But There's a mistake in my code and it always gives the wrong titels. After log in, I need to see "home, profiel and Log Out" but I keep seeing "home, log in and registreer". So I think there's a mistake with my loggedin(). Can someone please help me? :)

part of login

<?php
if (loggedin()) {
?>
<a href='index.php'>Home</a>
<a href='profile.php'>Profiel</a>
<a href='logout.php'>Log Out</a>
<?php
}else{  
?>
<a href='index.php'>Home</a>
<a href='login.php'>Log in</a>
<a href='register.php'>Registreer</a>
<?php
}
>

<?php
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = md5($_POST['password']);
if(empty($username) or empty ($password)){
    echo"<p>Gelieve al de velden in te vullen.</p>";
    }else{
        $check_login = mysql_query("SELECT id, type FROM users WHERE username='$username' AND password = '$password'");
            if (mysql_num_rows($check_login)==1){
                $run = mysql_fetch_array($check_login);
                $user_id=$run['id'];
                $type = $run ['type'];
                if($type == 'd'){
                    echo "<p>Je account is nog niet geactiveerd. Je ontvangt een mail zodra dit in orde is. Als je na lange tijd nog steeds niet kunt inloggen, neem dan contact op met één van de trainers.</p>";
                    }else{
                        $_SESSION['user_id'] = $user_id;
                        echo "<script> window.location.replace('membersonly.php') </script>";
                        }
                }else{
                    echo "<p>Ongeldige gebruikersnaam of wachtwoord.</p>";
                    }
        }
}
?>

function.php

<?php

session_start();

function loggedin() {
    if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) {
        return true;
    }else{
        return false;
    }
}

?>

part of membersonly.php

<?php include 'connect.php'; ?>

<?php include 'functions.php'; ?>

<?php

if (loggedin()) {
?>
<a href='index.php'>Home</a>
<a href='profile.php'>Profiel</a>
<a href='logout.php'>Log Out</a>
<?php
}else{  
?>
<a href='index.php'>Home</a>
<a href='login.php'>Log in</a>
<a href='register.php'>Registreer</a>
<?php
}
?>
  • 写回答

1条回答 默认 最新

  • douzhong1730 2014-04-29 12:56
    关注

    I cannot see your Form, but you should have it like this.

    <form action='PartOfLogin.php' method='POST'>
    Gebruikersnaam: <br/> 
    <input type= 'text' name='username' /> 
    <br/>
    <br/> 
    
    Wachtwoord: 
    <br/> 
    <input type= 'password' name ='password' /> 
    <br/>
    <br/> 
    
    <input type='submit' name='submit' value='Login' />
    </form>
    

    Part of Login. Also change 'or' in php for "||"

    <?php
    if(isset($_POST['submit'])){
    $username = $_POST['username'];
    $password = md5($_POST['password']);
    if(empty($username) || empty ($password)){
        echo"<p>Gelieve al de velden in te vullen.</p>";
        }else{
            $check_login = mysql_query("SELECT id, type FROM users WHERE username='$username' AND password = '$password'");
                if (mysql_num_rows($check_login)==1){
                    $run = mysql_fetch_array($check_login);
                    $user_id=$run['id'];
                    $type = $run ['type'];
                    if($type == 'd'){
                        echo "<p>Je account is nog niet geactiveerd. Je ontvangt een mail zodra dit in orde is. Als je na lange tijd nog steeds niet kunt inloggen, neem dan contact op met één van de trainers.</p>";
                        }else{
                            $_SESSION['user_id'] = $user_id;
                            echo "<script> window.location.replace('membersonly.php') </script>";
                            }
                    }else{
                        echo "<p>Ongeldige gebruikersnaam of wachtwoord.</p>";
                        }
            }
    }
    if (loggedin()) {
    ?>
    <a href='index.php'>Home</a>
    <a href='profile.php'>Profiel</a>
    <a href='logout.php'>Log Out</a>
    <?php
    }else{  
    ?>
    <a href='index.php'>Home</a>
    <a href='login.php'>Log in</a>
    <a href='register.php'>Registreer</a>
    <?php
    }
    >
    

    And change your mysql functions for mysli functions.
    Ref: http://www.w3schools.com/php/php_mysql_intro.asp

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题