2015-08-18 54 views
0

我想模擬一個登錄註銷(我是一個初學者),我必須使用會話和cookie。

因此,我使用了一個持續20秒的cookie,並記住用戶名和密碼,如果我在登錄時選中「記住我」選項。

並且還使用了一個會話。

但是,當我嘗試註銷時,如果我在第一個20秒內完成註冊,Mozzila會給我一個錯誤。 這是爲什麼?爲什麼會話結束時還會銷燬cookie?

這是日誌文件

<?php 
 
session_start(); 
 
$user="username"; 
 
$pass="password"; 
 

 
//verify if there is a cookie, and if so, log in from the cookie 
 
if(isset($_COOKIE['u']) && isset($_COOKIE['p']) && $_COOKIE['u']==$user && $_COOKIE['p']==$pass){ 
 
\t header("location: Index.php"); 
 
\t exit; 
 
\t } 
 

 
if(isset($_POST['sub'])) 
 
\t if(empty($_POST['name'])) echo "Please write username"; 
 
\t \t elseif(empty($_POST['pass'])) echo "Please write password"; 
 
\t \t else 
 
\t \t \t if($_POST['name']==$user && $_POST['pass']==$pass){ 
 
\t \t 
 
\t \t \t \t if($_POST['rem']=="on"){ 
 
\t \t \t \t \t setcookie("u", $_POST['name'],time()+10); 
 
\t \t \t \t \t setcookie("p", $_POST['pass'],time()+10); 
 
\t \t \t \t \t } 
 
\t \t \t \t \t 
 
\t \t \t \t $_SESSION['logged']="yes"; 
 
\t \t \t \t $_SESSION['name']=$_POST['name']; 
 
\t \t \t \t header("location: Index.php"); 
 
\t \t \t \t exit; 
 
\t \t \t \t } 
 
\t \t \t \t else echo "Incorrect data"; 
 

 

 
?> 
 

 

 

 

 
<html> 
 
<head> 
 
\t <title> Login </title> 
 
</head> 
 
<body> 
 
<form method="post"> 
 
<input type="text" name="name" placeholder="username" /><br /> 
 
<input type="password" name="pass" placeholder="password"/><br /> 
 
<input type="checkbox" name="rem" />Remember me<br /> 
 
<input type="submit" name="sub" value="Log in" /> 
 
</form> 
 
</html>

這是註銷:

<?php 
 
session_start(); 
 
$_SESSION=array(); 
 
session_destroy(); 
 
header("location: Login.php"); 
 
exit; 
 

 

 
?>

<?php 
 
session_start(); 
 

 
//verificam daca nu a ajuns fortat pe pagina, si ca mai intai s-a logat 
 
if(!isset($_SESSION['logged']) || $_SESSION['logged']!="yes"){ 
 
\t header("location: Login.php"); 
 
\t exit; 
 
\t } 
 

 
?> 
 

 

 
<html> 
 
<head> 
 
\t <title> Index </title> 
 
</head> 
 
<body> 
 
\t Welcome, <?php echo $_SESSION['name'];?>! 
 
\t <br /><br /><br /> 
 
\t <a href="Logout.php"> Log out! </a> 
 
</body> 
 
</html>

+0

什麼是錯誤 –

+0

的網頁沒有正確重定向 的Firefox已經檢測到服務器重定向此地址的請求的方式,將永遠不會完成。 此問題有時可能由禁用或拒絕接受Cookie引起。 –

+0

請發佈您的會話,cookie和註銷代碼 –

回答

0

Logout.php:

檢查該註銷腳本。在這裏,我正在過期cookie以停止重定向循環。保持所有腳本原樣。只更新logout.php這樣的:

<?php 

session_start(); 
$_SESSION=array(); 
session_destroy(); 
setcookie("u", $_POST['name'],time()-10); 
setcookie("p", $_POST['pass'],time()-10); 
header("location: Login.php"); 
exit; 


?> 
+0

但是這個餅乾對任何事物都有好處嗎? –

+0

做到了。刪除註銷頁面中的cookie。但是,如果在20秒內關閉mozzila然後重新打開索引,則會出現相同的錯誤。 (但是,註銷時沒有任何錯誤) –

+0

@LorelaySunshine檢查我更新的答案,希望它能解決您的問題:) –