如何删除真实的cookie? [英] How to delete a cookie for real?
问题描述
我设法在IE中严重破坏我的cookie字符串,它现在看起来像这样:
I've managed to mangle my cookie string in IE so badly, it now looks like this:
"; __atuvc=4%7C7; PHPSESSID=e4db10eb5d4409ba3203a7c1d533fafd; PHPSESSID=75a04bdcf604dd607d383da774c0f72a; __utmc=51433896; __utma=51433896.100703801.1392175783.1392175783.1392178863.2; __utmb=51433896.5.10.1392178863; __utmz=51433896.1392175783.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __atuvc=3%7C7"
这是键入 document.cookie
进入IE11开发者栏。
That's the output of typing document.cookie
into the IE11 developer bar.
您会注意到 PHPSESSID
是在那里两次。我想杀了它。
You will notice PHPSESSID
is in there twice. I want to kill it.
我试过在JavaScript中运行它:
I've tried running this in JavaScript:
document.cookie = 'PHPSESSID=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
并在PHP中运行:
session_start();
setcookie('PHPSESSID', '', time()-3600);
setcookie('PHPSESSID', '', time()-3600, '/');
session_unset();
session_destroy();
但似乎没有什么可以摆脱cookie。
But nothing seems to get rid of the cookie.
我知道我可以通过浏览器手动删除cookie,但是现在有很多用户都在炒相,我需要一种方法自动完成。
I'm aware I can delete the cookies manually through the browser, but a whole bunch of users have fried sessions right now, and I need a way to do it automatically.
推荐答案
删除cookie时,必须完全像设置时一样指定cookie参数。您可以使用 session_get_cookie_params
函数获取当前会话cookie的参数
When you delete a cookie you must specify cookie params exactly like when it were set. You can use session_get_cookie_params
function to get current session cookie's params
$params = session_get_cookie_params();
setcookie(
session_name(), '', time() - 86400,
$params['path'],
$params['domain'],
$params['secure'],
$params['httponly']
);
session_destroy();
这篇关于如何删除真实的cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!