php loggedin()提供错误的标题

时间:2014-04-29 12:04:40

标签: php login members

我是php的新手,所以我按照WellBro的教程进行操作。我正在进行注册和登录。但是我的代码中有一个错误,它总是给出错误的标题。登录后,我需要查看" home,profiel和Log Out"但我一直看到"回家,登录和注册"。所以我认为我的loggedin()出现了错误。有人可以帮帮我吗? :)

登录的一部分

<?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;
    }
}

?>

memberonly.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 个答案:

答案 0 :(得分:0)

我看不到你的表格,但是你应该这样看。

<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>

登录的一部分。同时更改&#39;或&#39;在php for&#34; ||&#34;

<?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
}
>

更改mysql函数的mysql函数。
Ref:http://www.w3schools.com/php/php_mysql_intro.asp