[英]php cache vs cookie
我正在集成一個登錄頁面(固定的用戶名和密碼)。
用戶登錄后,他將被重定向到另一個頁面“ x”(在我的服務器上)。
但是,當用戶關閉瀏覽器(或選項卡)並重新打開瀏覽器時,無需輸入用戶名和密碼即可自動將其定向到頁面“ x”。
但是,如果我從瀏覽器(firefox)設置中刪除了cookie,一切就會恢復正常。 刪除緩存不會執行任何操作。
我知道我需要插入幾行代碼才能刪除到Cookie。 我的問題是
下面是我的代碼。
<?php
session_start();
if(isset($_POST['username'])){
if(($_POST['username'] == "user") && ($_POST['password'] == "pass"))
{
$_SESSION['secured'] = "Secured";
}else{
echo "Wrong username and password. <p>
<a href='?'retry</a>";
}
}
if(!isset($_SESSION['secured']))
{
echo "<form method='post'>
Username: <input type='text' name='username' maxlength='10' /><br>
Password: <input type='password' name='password' maxlength='10' /><br>
<input type='submit' value='login' />
</form>";
}else{
?>
<html>
<head>
<title>Session Login</title>
</head>
<body>
<p>redirecting....
<meta HTTP-EQUIV="REFRESH" content="1; url=http://x.php">
</p>
</body>
</html>
<?php
}
?>
如果您可以創建一個logout.php頁面來破壞會話:
unset($_SESSION['secured']);
header('Location: login.php');
exit;
只需訪問該頁面,登錄名將被破壞。
如果您希望會話在預定的時間段后超時,則可以使用類似於本示例中所示的代碼。
如果您想在用戶登陸到x.php之后終止會話
<?php
session_start();
//First make sure that they're allowed access to x.php
if(!isset($_SESSION['secured'])){
//They shouldn't be here.
header('Location: login.php'); //Redirect back to your login page
exit;
}
//Ok, user is obviously logged in. Unset the session variable so that they can only view this page once (unless they login again)
unset($_SESSION['secured']);
//Show content of x.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.