php 删除PHP Cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6319135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete PHP Cookie?
提问by Jason
I currently have a cookie set as follows:
我目前有一个 cookie 设置如下:
setcookie("username",$username,time()+3600*24*5);
How would I go about clearing the value of that cookie so that the user's username isn't filled in anymore?
我将如何清除该 cookie 的值,以便不再填写用户的用户名?
I have it cleared as follows:
我已清除如下:
setcookie("username","",time()-60000);
The user's username still comes up though.
用户的用户名仍然出现。
The HTML form:
HTML 表单:
<?php
session_start();
$username = NULL;
$password = NULL;
if(isset($_SESSION['username'])){
$username = $_COOKIE['username'];
$password = $_COOKIE['password'];
}
?>
<html>
<title>Login</title>
<body bgcolor='#000000'>
<font color="white">
<H2><div align='center'>Login</div></H2>
<form align='center' action='login.php' method='POST'>
Username: <input type='text' name='username' value='<?$_COOKIE['username']?>'><br \>
Password: <input type='password' name='password' value='<?$password?>'><br \>
Remember Me <input type='checkbox' name='remember' value='rememberme'><br \>
<input type='submit' value='Login'>
</form>
</font>
</body>
</html>
The PHP script to handle the form:
处理表单的 PHP 脚本:
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
//Hash password in a new variable
$password2 = md5($password);
require_once "/home/a7435766/public_html/scripts/dbconnect.php";
$query = mysql_query("SELECT * FROM userstwo WHERE username = '$username' && password = '$password2'");
if((mysql_num_rows($query)) != 0) {
//Store username and password in a cookie
if($_POST['remember'] == 'rememberme') {
setcookie("username",$username,time()+3600*24*5,'','.ohjustthatguy.com');
setcookie("password",$password,time()+3600*24*2,'','.ohjustthatguy.com');
} else {
setcookie("username","",time()-10,'','.ohjustthatguy.com');
setcookie("password","",time()-10,'','.ohjustthatguy.com');
}
$_SESSION['username'] = $username;
header('Location: http://www.ohjustthatguy.com/uploads/uploads.html');
} else {
header('Location: http://www.ohjustthatguy.com/uploads/');
}
?>
Original sources on pastebin
pastebin 上的原始来源
回答by Chandranshu
Be sure that you delete the cookie with the same domain name and path with which you set it. Cookies for example.com and www.example.com will be treated as two different cookies. Similarly, cookies set from example.com and example.com/Support will have different paths. A good practice is to use .example.com as the domain and '/' as the path for username type cookies so that they can be shared across your subdomains too.
请确保删除与您设置的域名和路径相同的 cookie。example.com 和 www.example.com 的 cookie 将被视为两个不同的 cookie。同样,从 example.com 和 example.com/Support 设置的 cookie 将具有不同的路径。一个好的做法是使用 .example.com 作为域,使用“/”作为用户名类型 cookie 的路径,以便它们也可以在您的子域之间共享。
To debug this, you can use the FireCookie plugin of Firefox which'll show all this information.
要对此进行调试,您可以使用 Firefox 的 FireCookie 插件,它会显示所有这些信息。
回答by Christopher Armstrong
Setting its expiration to some time in the past should clear it:
将其过期时间设置为过去的某个时间应该可以清除它:
setcookie("username",$username,time()-10);
If you're using PHP sessions to manage users, you'll probably also want to session_destroy()
如果您使用 PHP 会话来管理用户,您可能还想 session_destroy()
回答by Fran?ois Lavigne
You really should not store your users password in a cookie, especially if you are not using HTTPS! The password will be sent in plaintext over the network for every requests! Also, never send back a user his password, this is nerver a good idea.
您真的不应该将您的用户密码存储在 cookie 中,尤其是在您不使用 HTTPS 的情况下!对于每个请求,密码将以明文形式通过网络发送!此外,永远不要向用户发回他的密码,这是一个好主意。