我似乎无法在Chrome中删除,过期或至少更新Cookie。我正在使用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>
答案 0 :(得分:1)
//This logs the person out if they enter "logout" as the username and password (testing purposes)
if(!isset($_COOKIE['user']))
因此,只有当他们没有登录时才将其注销...需要我说更多吗?