简体   繁体   English

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

[英]user authentication using sessions causes redirect loop

I am trying to make a authentication script, to redirect logged/unlogged users to proper page but its causing redirect loop. 我正在尝试制作身份验证脚本,以将已登录/未登录的用户重定向到正确的页面,但这会导致重定向循环。 I have two page index.php which has the login form and the mobile.php which has the contents for valid users. 我有两个页面index.php具有登录表单,而mobile.php具有有效用户的内容。 For authentication i have this code on the very first lines of index.php 为了进行身份验证,我在index.php的第一行上有此代码

session_start();
if (isset($_SESSION["username"]) && isset($_SESSION["userid"])) {
    header("location: mobile.php");
    exit();
}

and the very first lines of mobile.php is 而mobile.php的第一行是

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

But I dont know why its causing a redirect loop. 但我不知道为什么会导致重定向循环。 Here are some screenshots. 这是一些屏幕截图。 Thanks for your time. 谢谢你的时间。 在这里您可以看到重定向这是正在发送的标题

Here are some codes from index.php, these codes lines are followed by the form for login. 这是来自index.php的一些代码,这些代码行后跟用于登录的形式。 In the validate function all I do is check if the username and password is valid, if yes then i regenerate session id, and set session variable username and userid. 在validate函数中,我要做的就是检查用户名和密码是否有效,如果是,则重新生成会话ID,并设置会话变量username和userid。 and return true. 并返回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.';
    }           
}

Allways exit after location header. 在位置标头之后始终退出。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM