使用会话的用户身份验证导致重定向循环

时间:2014-12-03 08:40:55

标签: php

我正在尝试创建一个身份验证脚本,将已记录/未记录的用户重定向到正确的页面但会导致重定向循环。我有两个页面index.php,其中包含登录表单和mobile.php,其中包含有效用户的内容。对于身份验证,我在index.php

的第一行有这个代码
session_start();
if (isset($_SESSION["username"]) && isset($_SESSION["userid"])) {
    header("location: mobile.php");
    exit();
}

并且mobile.php的第一行是

session_start();
if(!isset($_SESSION["username"])){ 
    header("location: index.php"); 
}

但我不知道为什么会导致重定向循环。 这是一些截图。 谢谢你的时间。 here you can see the redirects here are the headers being sent

以下是index.php中的一些代码,这些代码行后面跟着登录表单。在validate函数中我所做的就是检查用户名和密码是否有效,如果是,则重新生成会话ID,并设置会话变量username和userid。并返回true。

if(isset($username) && isset($password)){
    $username = preg_replace('/[^a-zA-Z0-9]/', '', $username);  
    $password = preg_replace('/[^a-zA-Z0-9]/', '', $password);
    $returnmsg = validate($username,$password);
    if($returnmsg===true){
        // header("location: mobile.php");
        exit;
    } else {
        $returnmsg = 'Invalid username or password.';
    }           
}

1 个答案:

答案 0 :(得分:0)

总是在位置标题后退出。

session_start();
if(!isset($_SESSION["username"])){ 
   header("location: index.php"); 
   exit();
}
相关问题