PHP cookie 写入错误的域
我有一个在我的应用程序上使用的 cookie。它看起来像这样:
+-------+-------+-----------------------+-------+----------+
| Name | Value | Domain | Path | Expires |
+-------+-------+-----------------------+-------+----------+
| foo | bar | my.domain.tld | / | Session |
+-------+-------+-----------------------+-------+----------+
在我的脚本的一部分中,根据某些条件,我试图更改 cookie 的值。我正在使用这段代码:
// overwrite cookie
if($condition){
setcookie("foo", "cat", 0, "/", "my.domain.tld");
}
之后,我的 cookie 数据如下所示:
+-------+-------+-----------------------+-------+----------+
| Name | Value | Domain | Path | Expires |
+-------+-------+-----------------------+-------+----------+
| foo | bar | my.domain.tld | / | Session |
| foo | cat | .my.domain.tld | / | Session |
+-------+-------+-----------------------+-------+----------+
为什么 .
会被添加到域前面?我想覆盖现有的cookie。
I have a cookie that I use on my app. It looks like this:
+-------+-------+-----------------------+-------+----------+
| Name | Value | Domain | Path | Expires |
+-------+-------+-----------------------+-------+----------+
| foo | bar | my.domain.tld | / | Session |
+-------+-------+-----------------------+-------+----------+
In a section of my script, based on some condition, I'm trying to change the value of a cookie. I'm using this code:
// overwrite cookie
if($condition){
setcookie("foo", "cat", 0, "/", "my.domain.tld");
}
Afterward, my cookie data looks like this:
+-------+-------+-----------------------+-------+----------+
| Name | Value | Domain | Path | Expires |
+-------+-------+-----------------------+-------+----------+
| foo | bar | my.domain.tld | / | Session |
| foo | cat | .my.domain.tld | / | Session |
+-------+-------+-----------------------+-------+----------+
How come a .
is be prepended to the domain? I want to overwrite the existing cookie.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://www.php.net/manual/en/function。 setcookie.php#93641
答案在 php 手册的一篇文章中讨论。
Cookie 数据由浏览代理设置,因此根据浏览器使用的进程进行不同的处理。
http://www.php.net/manual/en/function.setcookie.php#93641
The answer is discussed in a post on the php manual.
Cookie data is set by the browsing agent, and so is handled differently depending on the process the browser uses.
来自文档:
cookie 可用的域。要使 cookie 在 example.com 的所有子域上可用,您可以将其设置为“.example.com”。这 。不是必需的,但可以使其与更多浏览器兼容。将其设置为 www.example.com 将使 cookie 仅在 www 子域中可用。有关详细信息,请参阅 » 规范中的尾部匹配。
尾部匹配规范在这里:
http://curl.haxx.se/rfc/cookie_spec。 html
From the documentation:
The domain that the cookie is available. To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. The . is not required but makes it compatible with more browsers. Setting it to www.example.com will make the cookie only available in the www subdomain. Refer to tail matching in the » spec for details.
And the tail matching spec is here:
http://curl.haxx.se/rfc/cookie_spec.html
事实证明,不指定域似乎有效:
预期的 cookie 数据:
奇怪,但它有效。
As it turns out, specifying no domain seems to work:
Expected cookie data:
Strange, but it works.