PHP setcookie 域

发布于 2024-08-21 13:02:12 字数 336 浏览 4 评论 0原文

某些应用程序(不是我编写的,也不是用 PHP 编写的)为 www.example.com 域创建了一个 cookie。

我正在尝试更换那个cookie。所以在 PHP 中我做了:

setcookie('mycookie','mydata',time() + 2*7*24*60*60,'/','www.example.com', false);

然而,生成的 cookie 是为域创建的:.www.example.com,请注意点“。”位于域之前。

所以它不会替换它,而是创建另一个 cookie。 我能做些什么?

Some application, not written by me, and not in PHP, creates a cookie for the domain www.example.com.

I am trying to replace that cookie. So in PHP I did:

setcookie('mycookie','mydata',time() + 2*7*24*60*60,'/','www.example.com', false);

However the resulting cookie is created for domain: .www.example.com, note the dot "." ahead of the domain.

So it doesn't replace it, it creates another cookie.
What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

落在眉间の轻吻 2024-08-28 13:02:12

该问题也在这里得到解决:
https://www.php.net/manual/en/function.setcookie。 php

请参阅 jah 的评论:

如果您想将 cookie 限制为单个主机,请提供空字符串形式的域参数

您也可以尝试使用 .example.com 作为域。尾随点将允许 example.com 的所有子域使用 cookie,并且可能会覆盖 www.-cookie,但我将首先采用上述解决方案。

The issue is also adressed here:
https://www.php.net/manual/en/function.setcookie.php

See comment by jah:

If you want to restrict the cookie to a single host, supply the domain parameter as an empty string

You could also try .example.com as the domain. The trailing dot will allow a cookie for all subdomains for example.com and could overwrite the www.-cookie, but I'll go with the above solution first.

愿与i 2024-08-28 13:02:12

如果指定域,则应遵循 RFC 2109 并在域前添加点前缀;否则客户就会这样做。但如果您根本不指定域,客户端将采用请求的域。

If you specify a domain, you should follow RFC 2109 and prefix the domain with a dot; otherwise the client will do that. But if you don’t specify a domain at all, the client will take the domain of the request.

老街孤人 2024-08-28 13:02:12

尝试创建多个具有相同名称但不同域的其他 cookie。示例:

setcookie('mycookie','mydata1',time() + 2*7*24*60*60,'/','www.example.com', false);
setcookie('mycookie','mydata2',time() + 2*7*24*60*60,'/','www.example.com', false);
setcookie('mycookie_top','mydata1',time() + 2*7*24*60*60,'/','example.com', false);
setcookie('mycookie_top','mydata2',time() + 2*7*24*60*60,'/','example.com', false);

然后在 Firebug 中检查这些命令创建的 cookie。如果您不断收到双 cookie,那么这可能是 PHP 中的一个错误。另外,尝试在JavaScript代码中设置cookie,看看是否仍然遇到同样的问题。

Try to create several other cookie with same name, but a different domain. Example:

setcookie('mycookie','mydata1',time() + 2*7*24*60*60,'/','www.example.com', false);
setcookie('mycookie','mydata2',time() + 2*7*24*60*60,'/','www.example.com', false);
setcookie('mycookie_top','mydata1',time() + 2*7*24*60*60,'/','example.com', false);
setcookie('mycookie_top','mydata2',time() + 2*7*24*60*60,'/','example.com', false);

Then inspect the cookie created by these command in the Firebug. If you kept getting a double cookie, then this might be a bug in the PHP. Also, try to set the cookie in the JavaScript code, see if you still got the same problems.

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