如何设置仅对特定域(例如 example.com)有效但对其子域无效的 cookie?
如果我有一个域 example.com
,有什么方法可以使 cookie 仅对该特定域有效,而不对 www.example.com
等子域有效?
我知道我只能将其设置为 www.example.com
,但它可以没有子域吗?
if I have a domain example.com
, is there any way to make cookies valid only for that specific domain and not for sub-domains like www.example.com
?
I know I can set it to only www.example.com
, but can it be without a sub-domain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Cookie 通过名称、域和路径的组合来识别。因此,如果设置正确,您可以将其范围限制为特定域/子域和路径。
阅读 Wiki 的 HTTP Cookie 的属性部分
或 RFC2965
Cookies are identified by the combination of their name, domain, and path. So if set correctly, you can limit it's scope to a specific domain/sub-domain and path.
Read Wiki's HTTP Cookie's Attribute Section
or RFC2965
严格来说,带有限定符
";domain=example.com"
的 cookie 对于“www.domain.com”域不应该可见。而";domain=.example.com"
对 www 主机可见。不过我对此会非常警惕。我最近没有测试过这一点,但如果看到某些浏览器不符合这一点,我不会感到惊讶。
Strictly speaking a cookie carrying the qualifier
";domain=example.com"
should not be visible to the "www.domain.com" domain. Whereas";domain=.example.com"
would be visible to the www host.However I would be very wary of this. I haven't tested this recently but I wouldn't be surprised to see some browsers not conforming properly to this.