3

我不知道哪里出错了。当我勾选记住我复选框页面没有转到用户区域页面并且似乎 cookie 无法正常工作当我只登录而不勾选记住我复选框时它可以工作

<?PHP
session_start();

$connect=mysql_connect("localhost", "root", "");
mysql_select_db("phplogin", $connect);


function loggedin(){
    if(isset($_SESSION['email']) || isset($_COOKIE['username'])){
    $loggedin=TRUE;
    return $loggedin;   
    }
}

if(loggedin()){
header("Location:userarea.php");}

if(isset($_POST['login'])){

$email=$_POST['email'];
$password=$_POST['password'];
$rememberme=$_POST['rememberme'];

    if($email && $password){
        $login=mysql_query("SELECT * FROM users WHERE email='$email'");
            while($row=mysql_fetch_assoc($login)){
            $db_email=$row['email'];
            $db_password=$row['password'];
            $db_firstname=$row['firstname'];
            $db_lastname=$row['lastname'];  

            if($password==$db_password){$log=TRUE;}
            else{$log=FALSE;}               
            }

            if($log==TRUE){
                if($rememberme=="on"){
                    setcookie("email", $email, time()+7200);
                }
                else if($rememberme==""){
                    $_SESSION['email']=$email;
                    header("Location:userarea.php");
                }
            }
            else{die("Wrong email or password");}
    }
}
?>

<html>
    <form action="index.php" method="POST">
        <input type="text" name="email" value="" placeholder="email" /><br/>
        <input type="password" name="password" value="" placeholder="password"/><br/>

        <input type="checkbox" name="rememberme"> Remember me | <a href="register.php">Register?</a><br/>
         <input type="submit" name="login" value="Login" />
     </form>
</html>
4

2 回答 2

4

函数 login() 中的 cookie 变量名称错误。它应该是 isset($_COOKIE['email'])

function loggedin(){
    if(isset($_SESSION['email']) || isset($_COOKIE['email'])){
       $loggedin=TRUE;
       return $loggedin;   
    }
}
于 2012-12-06T09:33:03.197 回答
0

改变如下

if(isset($_POST['rememberme'])){
    $rememberme = $_POST['rememberme'];
}

然后

if($log==TRUE){
    if(isset($rememberme)){
        setcookie("email", $email, time()+7200);
    }
    else{
        $_SESSION['email']=$email;  
    }
    header("Location:userarea.php");
    exit();
}
于 2012-12-06T10:02:53.190 回答