cookie 不会取消设置
我无法取消设置 cookie。
cookie 设置:(id、别名)
setcookie("id",$data['id'], time()+3600*24*30);
setcookie("alias",$this->nombre, time()+3600*24*30);
cookie 未设置? (id,别名)
setcookie("id","-1",time()-315360000);
setcookie("alias","",time()-315360000);
unset($_COOKIE['id']); // additional, but still no..
unset($_COOKIE['alias']); // " "
我做错了什么?
I am unable to get the cookie to unset.
cookie set: (id, alias)
setcookie("id",$data['id'], time()+3600*24*30);
setcookie("alias",$this->nombre, time()+3600*24*30);
cookies unset? (id, alias)
setcookie("id","-1",time()-315360000);
setcookie("alias","",time()-315360000);
unset($_COOKIE['id']); // additional, but still no..
unset($_COOKIE['alias']); // " "
What I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将时间设置为一小时前,而不是那么大的数字怎么样?
How about setting the time to an hour back, rather than that large number?
使用您的示例,我创建了此测试:
在第一次加载时,它输出:
在重新加载时:
在第二次重新加载时:
所以看来您的代码正在重置往返中的 cookie。
编辑
以下内容:
将打印:
或将打印:
http://jfcoder。 com/test/cookies.php(点击重新加载几次)
如果您需要告诉浏览器忘记 cookie,请使用
setcookie()
并及时设置时间(我至少使用24小时)。如果您需要$_COOKIES
数组来忘记该值,请使用unset()
。编辑
这里可能存在两个问题,一是 cookie 上的子域不匹配,二是路径可访问性问题。
例如...
如果访问者访问的 url 与尝试重置 cookie 的 url 位于不同的目录中,则需要使用允许其他人访问(和重置)该 cookie 的路径来设置 cookie路径。
或者允许子目录中包含的路径...
如果访问者访问的 url 是子域(包括 www),但您希望所有子域都可以访问 cookie,则需要为 setcookie 提供通配符。
将允许来自 www.example.com、my.example.com 和 sub.example.com 的 URL 访问和重置 cookie。显然,此时也需要考虑您的路径注意事项,因为对于子域参数,您将需要包含路径。
/
选择 url 上的所有子目录,而.
在域之前选择子域(虽然 sub.sub.domains,我不确定)。http://php.net/manual/en/function.setcookie.php
Using your example, I created this test:
On the first load, it outputs:
On reload:
On second reload:
So it appears your code is resetting the cookie on the roundtrip.
EDIT
The following:
Will either print:
Or will print:
http://jfcoder.com/test/cookies.php (hit reload a few times)
If you need to tell the browser to forget the cookie, use
setcookie()
with the time set back in time (I use at least 24 hours). If you need the$_COOKIES
array to forget the value, useunset()
.EDIT
There are two possible issues contributing here, one a subdomain mismatch on the cookie, and a path accessibility problem.
For instance...
If the url the visitor accessed was on a directory different from where the url that attempts to reset the cookie, you need to set the cookie with a path that will allow that cookie to be accessed (and reset) by other paths.
Or to allow for paths contained within a subdirectory...
If the url the visitor accessed was a subdomain (including www), but you want the cookie to be accessible to all subdomains, you need to give a wildcard to setcookie.
Will allow urls from www.example.com, my.example.com, and sub.example.com to access and reset the cookie. Obviously, at this point too your path considerations need to be taken into account, since for a subdomain argument, you will need to include a path.
/
selects all subdirectories on the url, and.
before the domain selects subdomains (although sub.sub.domains, I'm not sure).http://php.net/manual/en/function.setcookie.php