如何阻止子域使用域 cookie?
我有以下域的设置:
mydomain.com
www.mydomain.com
有一个问题(在 Internet Explorer 上测试): 如果为 mydomain.com 设置了某个 cookie,则即使我为 www.mydomain.com 设置了同名的 cookie,该 cookie 也对 www.mydomain.com 有效。
更具体的例子:
1)用户在 mydomain.com 网站上选择他喜欢的语言,我设置了 cookie usrlng=en
2)第二天,其他人使用同一台计算机,导航到 www.mydomain.com 并选择他的语言,我设置 usrlng=de。但 Internet Explorer 不断向服务器发送 cookie usrlng=en 和 usrlng=de (我在 Fiddler 中看到了这一点)!为什么它发送相同的 cookie 两次并且不使用子域值覆盖“usrlng”?
同时我看到子域的 PHPSESSID 被正确覆盖,没有两个 PHPSESSID cookie 被发送到服务器。
如何修复 usrlng cookie 并使其以与 PHPSESSID 相同的方式工作?
I have a setup with the following domains:
mydomain.com
www.mydomain.com
There is one problem (tested on Internet Explorer):
if some cookie is set for mydomain.com, this cookie is also effective for www.mydomain.com even if I set a cookie with the same name for www.mydomain.com.
More specific examople:
1) the user chooses his prefered language on website mydomain.com and I set the cookie usrlng=en
2) next day someone else uses the same computer, naviagtes to www.mydomain.com and chooses his language, and I set the usrlng=de. But Internet Explorer keeps sending both cookies usrlng=en and usrlng=de to the server (I see this in Fiddler)! Why is it sending the same cookie twice and not overriding 'usrlng' with the subdomain value?
At the same time I see that PHPSESSID is being overwritten correctly for the subdomain, there are no two PHPSESSID cookies being sent to the server.
How can I fix the usrlng cookie and make it work the same way as PHPSESSID works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以为每个设置不同的 save_path...这样它们就不会共享会话。
PHP 示例:
PHP 需要访问会话目录才能写入。
You can also set a different save_path for each... so they don't share the sessions.
PHP example:
PHP needs access to write in the sessions directory.
现在我通过设置cookie的“主机”而不是“域”解决了这个问题; 'host' 属性允许将 cookie 限制为 mydomain.com 或 www.mydomain.com。
也许这是唯一的方法,并且“域”不能设置为覆盖顶级域 cookie。
For now I solved the problem by setting the 'host' of the cookie instead of 'domain'; 'host' property allowed to limit the cookie to mydomain.com or www.mydomain.com.
Maybe that is the only way to go and 'domain' cannot be set up to oveeride top level domain cookies.