2014-02-25 112 views
0

我在php中使用會話和cookie進行了登錄系統。標題不與會話一起工作

登錄後,我希望用戶按順序瀏覽頁面,通過使用後退按鈕或輸入url(即使它們已登錄),防止它們返回上一頁或任何頁面。除了第一個信息頁面,每個頁面都有一個問題,只有正確的答案才能讓你進入下一頁,就像尋寶。爲此,我爲每個頁面使用會話變量。

登錄(用戶 - area.php)後

第一頁:

<?php 
session_start(); 
require_once("user.cookies.php"); 

$firstname_user = $_SESSION["SESS_USERFIRSTNAME"]; 
?> 

/*html part*/ 
<a href="q1.php">Question 1</a><br><br> 
<?php 
    $_SESSION["from_start"] = "true"; 
?> 

<a href="logout.php">Logout</a> 

第二頁(q1.php)後:

<?php 
session_start(); 
require_once("user.cookies.php"); 

$firstname_user = $_SESSION["SESS_USERFIRSTNAME"]; 

if (isset($_POST["ans1"])) {   //if answer is submitted 
    if ($_POST["ans1"]=="correct") {   //if answer is correct 
     $_SESSION["from_q1"] = "true"; //for verifying in next page, q2.php 
     header('Location: q2.php');  //heading to next page 
    } 
    else{         //if answer is not correct 
     $_SESSION["from_start"] = "true"; //same page loads 
    } 

} 

if($_SESSION['from_start'] == "false"){ //to check that it came from users-area.php 
    //if not, logout 
    header("Location: logout.php"); 
} 
else{    
    //if yes,reset the variable so that they can't access from anywhere else anymore 
    $_SESSION['from_start'] = "false"; 
} 
?> 

//html part 
<form method="POST"> //sending input to same page 
    <input type="text" name="ans1"> 
    <input name="submit" type="submit" value="Sumbit"> 
</form> 
<a href="logout.php">Logout</a> 

該頁面後(q2.php):

<?php 
session_start(); 
require_once("user.cookies.php"); 

$firstname_user = $_SESSION["SESS_USERFIRSTNAME"]; 

if($_SESSION['from_q1'] == "false"){ 
    header("Location: logout.php"); 
} 
else{ 
    $_SESSION['from_q1'] = "false"; 
} 

} 
?> 

登錄系統工作良好。但是,在添加所有這些限制後,在q1.php中,提交正確答案會導致註銷。即使我已經把header('Location: q2.php');放在任何與註銷有關的東西之前。提交正確答案應該導致q2.php。

其他所有工作。 q1.php中的錯誤答案只按預期重新加載頁面。並試圖通過後退按鈕或輸入網址去任何頁面導致註銷。

如果有幫助,這裏是user.cookies.php:

<?php 
    //redirect function 
    function returnheader($location){ 
    $returnheader = header("location: $location"); 
    return $returnheader; 
    } 

    if(!strlen($_SESSION["SESS_USERNAME"])){ 
    //redirect 
    returnheader("index.php"); 
    } 
?> 

爲什麼不q1.php與正確答案正常工作?它是不是一個接一個地被使用?

+0

把'exit'放在''header'函數後.... –

+0

工作,謝謝。 –

回答

1

After header('Location:q2.php');您必須退出腳本,否則執行將繼續執行。

if (isset($_POST["ans1"])) {   //if answer is submitted 
    if ($_POST["ans1"]=="correct") {   //if answer is correct 
     $_SESSION["from_q1"] = "true"; //for verifying in next page, q2.php 
     header('Location: q2.php');  //heading to next page 
     exit; 
    } 
    else{         //if answer is not correct 
     $_SESSION["from_start"] = "true"; //same page loads 
    } 

}