php 删除一个 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/686155/
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
Remove a cookie
提问by sanders
When I want to remove a Cookie I try
当我想删除 Cookie 时,我尝试
unset($_COOKIE['hello']);
I see in my cookie browser from firefox that the cookie still exists. How can I really remove the cookie?
我在 Firefox 的 cookie 浏览器中看到 cookie 仍然存在。我怎样才能真正删除cookie?
回答by Re0sless
Set the value to "" and the expiry date to yesterday (or any date in the past)
将值设置为 "" 并将到期日期设置为昨天(或过去的任何日期)
setcookie("hello", "", time()-3600);
Then the cookie will expire the next time the page loads.
然后 cookie 将在下次加载页面时过期。
回答by Nikunj K.
You May Try this
你可以试试这个
if (isset($_COOKIE['remember_user'])) {
unset($_COOKIE['remember_user']);
setcookie('remember_user', null, -1, '/');
return true;
} else {
return false;
}
回答by Mouloud
A clean way to delete a cookie is to clear both of $_COOKIEvalue and browser cookie file :
删除 cookie 的一种干净方法是清除$_COOKIE值和浏览器 cookie 文件:
if (isset($_COOKIE['key'])) {
unset($_COOKIE['key']);
setcookie('key', '', time() - 3600, '/'); // empty value and old timestamp
}
回答by Thejoker123
To reliably delete a cookie it's not enough to set it to expire anytime in the past, as computed by your PHP server. This is because client computers can and often do have times which differ from that of your server.
要可靠地删除 cookie,仅将其设置为过去任何时间(由您的 PHP 服务器计算)过期是不够的。这是因为客户端计算机可以而且经常确实有与您的服务器不同的时间。
The best practice is to overwrite the current cookie with a blank cookie which expires one second in the futureafter the epoch (1 January 1970 00:00:00 UTC), as so:
最佳做法是用一个空白 cookie 覆盖当前 cookie,该 cookie在纪元(1970 年 1 月 1 日 00:00:00 UTC)之后的未来一秒到期,如下所示:
setcookie("hello", "", 1);
回答by Eric Petroelje
That will unset the cookie in your code, but since the $_COOKIE variable is refreshed on each request, it'll just come back on the next page request.
这将在您的代码中取消设置 cookie,但由于 $_COOKIE 变量在每次请求时都会刷新,因此它只会在下一个页面请求时返回。
To actually get rid of the cookie, set the expiration date in the past:
要真正摆脱 cookie,请将过期日期设置为过去:
// set the expiration date to one hour ago
setcookie("hello", "", time()-3600);
回答by user3285097
I had the same problem in my code and found that it was a cookie path issue. Check out this stack overflow post: Can't delete php set cookie
我在我的代码中遇到了同样的问题,发现这是一个 cookie 路径问题。查看此堆栈溢出帖子:无法删除 php set cookie
I had set the cookie using a path value of "/", but didn't have any path value when I tried to clear it, so it didn't clear. So here is an example of what worked:
我已经使用路径值“/”设置了 cookie,但是当我尝试清除它时没有任何路径值,所以它没有清除。所以这是一个有效的例子:
Setting the cookie:
设置cookie:
$cookiePath = "/";
$cookieExpire = time()+(60*60*24);//one day -> seconds*minutes*hours
setcookie("CookieName",$cookieValue,$cookieExpire,$cookiePath);
Clearing the cookie:
清除cookie:
setcookie("cookieName","", time()-3600, $cookiePath);
unset ($_COOKIE['cookieName']);
Hope that helps.
希望有帮助。
回答by Alistair Knock
If you set the cookie to expire in the past, the browser will remove it. See setcookie() delete example at php.net
如果您过去将 cookie 设置为过期,浏览器会将其删除。请参阅php.net 上的 setcookie() 删除示例
回答by Jarret Hardie
See the sample labelled "Example #2 setcookie() delete example" from the PHP docs. To clear a cookie from the browser, you need to tell the browser that the cookie has expired... the browser will then remove it. unsetas you've used it just removes the 'hello' cookie from the COOKIE array.
请参阅PHP 文档中标有“示例 #2 setcookie() 删除示例”的示例。要从浏览器中清除 cookie,您需要告诉浏览器 cookie 已过期……然后浏览器将删除它。unset正如您所使用的那样,它只是从 COOKIE 数组中删除了 'hello' cookie。
回答by Nulik
This is how PHP v7 setcookie() code works when you do:
当您执行以下操作时,PHP v7 setcookie() 代码的工作方式如下:
<?php
setcookie('user_id','');
setcookie('session','');
?>
From the output of tcpdump while sniffing on the port 80, the server sends to the client (Browser) the following HTTP headers:
在嗅探端口 80 时,从 tcpdump 的输出中,服务器向客户端(浏览器)发送以下 HTTP 标头:
Set-Cookie: user_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0
Set-Cookie: session=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0
Observing packets in the following requests the Browser no longer sends these cookies in the headers
观察以下请求中的数据包,浏览器不再在标头中发送这些 cookie
回答by boksa
To delete cookie you just need to set the value to NULL:
要删除 cookie,您只需将该值设置为 NULL:
"If you've set a cookie with nondefault values for an expiration time, path, or domain, you must provide those same values again when you delete the cookie for the cookie to be deleted properly." Quote from "Learning PHP5" book.
“如果您为过期时间、路径或域设置了具有非默认值的 cookie,则在删除 cookie 时必须再次提供这些相同的值,以便正确删除 cookie。” 引自“Learning PHP5”一书。
So this code should work(works for me):
所以这段代码应该工作(对我有用):
Setting the cookie:
setcookie('foo', 'bar', time() + 60 * 5);
设置cookie:
setcookie('foo', 'bar', time() + 60 * 5);
Deleting the cookie:
setcookie('foo', '', time() + 60 * 5);
删除cookie:
setcookie('foo', '', time() + 60 * 5);
But i noticed that everybody is setting the expiry date to past, is that necessary, and why?
但我注意到每个人都将到期日期设置为过去,这是必要的,为什么?

