PHP不能删除由JavaScript设置的cookie

问题描述:

是否可以通过使用PHP在JS上删除设置的Cookie?

Is it possible to remove cookie that was set on front via JS with PHP?

我这样做:

* FRONT(JS):

if ($.cookie('myCookie'))
{
   console.log('Cookie.. :(  ');
}
else
{
    console.log('Yaay! No cookie!');
    $.cookie('myCookie', '123');
}

BACK(PHP)

if (isset($_REQUEST['removeCookie']))
{
   setcookie("myCookie", "", time()-3600);
   unset($_COOKIE['myCookie']);
}

结果:

看起来像是mistery

您无法强制浏览器删除Cookie文件,但您可以删除cookie的内容,并过期它。这正是你正在使用上面的代码。我可能会稍微调整一下:

You can't force the browser to delete the cookie file. You can, however, delete the contents of the cookie and expire it. Which is exactly what you're doing with your code above. I would probably tweak it slightly:

setcookie('myCookie', '', 1, '/'); // no need to calculate one hour ago.