我似乎不能得到一个cookie删除,或过期,或至少在Chrome浏览器更新。我正在使用cookie的值来测试用户是否登录,并且没有cookie过期,或被删除,或值更改,它不会将用户注销。
下面是我在用户点击登出时使用的代码:
$_COOKIE['user'] = '';
unset($_COOKIE['user']);
setcookie('user', '', time() - (86400 * 30), "/", ".domain.com");
(第2行和第3行已按不同顺序试过,没有变化)
当我去到另一个页面,然后回到一个需要密码保护的页面,它仍然注册我登录。如果我在Chrome浏览器的设置中查看cookie,它会显示cookie内容和到期日期(从现在起一个月)不变。
更新包括页面的所有代码,希望对您有所帮助。
<?php
$preset_username = "username";
$preset_password = "userpassword";
//Setting values to nothing the first time page loads
$message = "";
$username = "";
$password = "";
$redirect = "";
$height = "height: 380px;";
//This code is run when someone clicks on the login button
if($_SERVER["REQUEST_METHOD"] == "POST") {
//Saves the username and password entered into the textboxes
$username = trim($_POST['user']);
$password = trim($_POST['pword']);
//Checks to see if the username and password entered match the preset username and password
if ($username == $preset_username && $password == $preset_password) {
if(!isset($_COOKIE['user']) || $_COOKIE['user'] == '') {
setcookie('user', 'true', time() + (86400 * 30), "/", ".domain.com"); //86400 = 1 day
$_COOKIE['user'] = 'true';
}
//Checks to see if there's a page saved that they should be redirected to once they've logged in
if (isset($_COOKIE['redirect_to'])) {
$redirect_address = $_COOKIE['redirect_to'];
//Performs redirection if using cookies
$redirect = "<script type='"text/javascript'">location.href = '";
$redirect .= $redirect_address;
$redirect .= "';</script>";
echo $redirect;
}
//Redirects to the page they came from
echo $redirect;
}
elseif ($username == "logout" && $password == "logout") {
//This logs the person out if they enter "logout" as the username and password (testing purposes)
if(!isset($_COOKIE['user'])) {
$_COOKIE['user'] = '';
unset($_COOKIE['user']);
setcookie('user', '', time() - (86400 * 30), "/", ".domain.com");
}
$height = "height: 400px;"; //Resets the height - Used with the css
$message = "<p style='"color: darkblue; text-align: center;'">You have been logged out</p>";
}
else {
//Resets the height - Used with the css
$height = "height: 440px;";
//Sets an error message if the person enters the wrong login information
$message = "<p style='"color: 4C4646; text-align: center;'">Incorrect Login <br /> Please check your login name and password and try again.</p>";
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" >
<head>
<title>REMOVED</title>
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
}
body {
background-image: url('http://domain.com/images/images2.jpg');
}
#container {
position: absolute;
<?php echo $height; ?>
width: 700px;
margin: 250px auto 0px auto;
top: 0px;
left: 0px;
bottom: 0px;
right: 0px;
background-image:url('http://domain.com/images/beige031.jpg');
}
#content {
width: 650px;
font-family: Times New Roman;
font-size: 18pt;
color: #3B3131;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div id="container">
<div id="content">
<p style="text-align: justify;">This page ...</p>
<?php echo $message; ?>
<div style="width: 700px;">
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p style="text-align: justify; padding-top: 10px;">
Login:<input name="user" type="text" />
<p style="text-align: justify;">
Password:<input name="pword" type="password" />
</p>
<input value="Login" name="submit" type="submit" />
</form>
</div>
</div>
</div>
</body>
</html>
//This logs the person out if they enter "logout" as the username and password (testing purposes)
if(!isset($_COOKIE['user']))
所以你只在他们没有登录的时候注销他们…还需要我多说吗?