php 从浏览器中删除cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1267341/
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 cookie from browser?
提问by mr-euro
Is there any way of instructing a web browser to completely deleteone's cookie set with PHP?
有什么方法可以指示网络浏览器完全删除使用 PHP 设置的 cookie 吗?
I do not want to expiryit or wait for the browser to be closed.
我不想让它过期或等待浏览器关闭。
With delete I mean to actually not have it listed inside the cookie's list anymore.
对于删除,我的意思是实际上不再将它列在 cookie 的列表中。
采纳答案by Rob
You cannot force the browser to delete the file associated with any cookie, because you can't guarantee there's actually such a file - the contract between browser and web server regarding cookies is that the data will be presented at eligible requests.
您不能强制浏览器删除与任何 cookie 相关联的文件,因为您不能保证确实存在这样的文件 - 浏览器和网络服务器之间关于 cookie 的合同是数据将在符合条件的请求时呈现。
You state that you "don't want to wait for the cookie to expire", but cookie expiration is the correct method for indicating that the data is no longer needed and should not be presented on future requests, which in most cases does translate to the browser removing the file.
您声明“不想等待 cookie 过期”,但 cookie 过期是指示不再需要数据且不应在未来请求中呈现的正确方法,在大多数情况下,这确实会转化为浏览器删除文件。
To delete a cookie, therefore, set its expiration time into the past. In PHP, this is done with setcookie().
因此,要删除 cookie,请将其过期时间设置为过去。在 PHP 中,这是通过setcookie().
回答by Gumbo
Try something like this to delete all cookies:
尝试像这样删除所有 cookie:
foreach ($_COOKIE as $name => $value) {
setcookie($name, '', 1);
}
The value 1is the expirevalue and it represents one second after the begin of the Unix time epoch. So it's always already expired.
该值1是到期值,它代表 Unix 时间纪元开始后的一秒。所以它总是已经过期了。
回答by ceejayoz
Yes. Use setcookie()and set the expiration date for the cookie you wish to delete to a time in the past. The user's browser should automatically remove it as a result.
是的。使用setcookie()您希望删除的 cookie 的过期日期并将其设置为过去的某个时间。因此,用户的浏览器应自动将其删除。
回答by Jerry Wickey
'Seems that deleting a cookie is more difficult than it looks.
'似乎删除 cookie 比看起来更困难。
setcookie($name, '', 1);
Won't do the trick. The ''is empty and setcookiecan ignore the whole instruction.
不会做伎俩。该''是空的,setcookie可以忽略整个指令。
Also setting the time to the past sometimes allows the cookie to retain the value whose expire time is newer than 1.
同样将时间设置为过去有时会允许 cookie 保留过期时间比 1 新的值。
I am dealing with this right now. I don't know where it comes from, but it's there.
我现在正在处理这个问题。我不知道它来自哪里,但它就在那里。
I've resorted to
我采取了
setcookie($name, '0', 9000000000);
This ensures the cookie is set to a value that resolves to false and that it is newer than any previous value.
这可确保将 cookie 设置为解析为 false 的值,并且它比任何以前的值都新。
If anyone has any insight into this behavior please tell.
如果有人对此行为有任何见解,请告诉。
I suspect the difficulty lies in the fact that the domain and path values for setcookieare guaranteed to be the same from execution to execution when the values are not specified.
我怀疑困难在于,setcookie当未指定值时,保证域和路径值从执行到执行是相同的。
And I am aware such a cookie will not expire until 2038 or so.
而且我知道这样的 cookie 直到 2038 年左右才会过期。
Alternately, if the newest expiration date of the cookie is known, it need be set only 1 second after.
或者,如果 cookie 的最新过期日期是已知的,则只需在 1 秒后设置。
回答by spuklo
I think that you have to use combined approach:
我认为您必须使用组合方法:
- set expiration way back in the past (as suggested by Chacha102)
- use JavaScriptto delete entries from document.cookie DOM object (as suggested by andres descalzo)
- 设置过期时间回到过去(如 Chacha102 所建议的)
- 使用 JavaScript 从 document.cookie DOM 对象中删除条目(如 andres descalzo 所建议的)
There are 2 good reasons for going with mixed approach:
使用混合方法有两个很好的理由:
- JavaScript can be disabled in the browser
- not all cookies are visible in document.cookie Some modern browsers are supporting httponly flag for cookies. PHP has support for httponly cookies, see http://www.php.net/setcookie
- 可以在浏览器中禁用 JavaScript
- 并非所有 cookie 在 document.cookie 中都可见 一些现代浏览器支持 cookie 的 httponly 标志。PHP 支持 httponly cookie,见http://www.php.net/setcookie
回答by andres descalzo
I wrote this plugin for me and works correctly.
我为我编写了这个插件并且工作正常。
(function($) {
$.cookieAllDelete = function(doc)
{
var cookie_date = new Date();
var cookies = null;
cookies = doc.cookie.split(';');
cookie_date.setTime(cookie_date.getTime() - 1);
for(var i=0; i < cookies.length; i++)
{
var cookie_name = cookies[i].split('=')[0];
try {
if (cookie_name.length > 0)
doc.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
} catch(ex) {}
}
}
})(jQuery);
jQuery.cookieAllDelete(document);

