我想模擬一個登錄註銷(我是一個初學者),我必須使用會話和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>
什麼是錯誤 –
的網頁沒有正確重定向 的Firefox已經檢測到服務器重定向此地址的請求的方式,將永遠不會完成。 此問題有時可能由禁用或拒絕接受Cookie引起。 –
請發佈您的會話,cookie和註銷代碼 –