PHP cookie 写入错误的域

发布于 2024-09-08 17:36:41 字数 1010 浏览 4 评论 0原文

我有一个在我的应用程序上使用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Smile简单爱 2024-09-15 17:36:41

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.

罗罗贝儿 2024-09-15 17:36:41

来自文档:

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

最丧也最甜 2024-09-15 17:36:41

事实证明,不指定域似乎有效:

setcookie("foo", "cat", 0, "/");

预期的 cookie 数据:

+-------+-------+-----------------------+-------+----------+
| Name  | Value | Domain                | Path  | Expires  |
+-------+-------+-----------------------+-------+----------+
| foo   | cat   | my.domain.tld         | /     | Session  |
+-------+-------+-----------------------+-------+----------+

奇怪,但它有效。

As it turns out, specifying no domain seems to work:

setcookie("foo", "cat", 0, "/");

Expected cookie data:

+-------+-------+-----------------------+-------+----------+
| Name  | Value | Domain                | Path  | Expires  |
+-------+-------+-----------------------+-------+----------+
| foo   | cat   | my.domain.tld         | /     | Session  |
+-------+-------+-----------------------+-------+----------+

Strange, but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文