簡體   English   中英

使用會話的用戶身份驗證導致重定向循環

[英]user authentication using sessions causes redirect loop

我正在嘗試制作身份驗證腳本,以將已登錄/未登錄的用戶重定向到正確的頁面,但這會導致重定向循環。 我有兩個頁面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"); 
}

但我不知道為什么會導致重定向循環。 這是一些屏幕截圖。 謝謝你的時間。 在這里您可以看到重定向這是正在發送的標題

這是來自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.';
    }           
}

在位置標頭之后始終退出。

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