在子域和域之间共享Cookie [英] Share cookie between subdomain and domain

查看:173
本文介绍了在子域和域之间共享Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题。我知道如果我在cookie中指定域为 .mydomain.com (带前导点),所有子域都可以共享一个cookie。

I have two questions. I understand that if I specify the domain as .mydomain.com (with the leading dot) in the cookie that all subdomains can share a cookie.

可以 subdomain.mydomain.com 访问在 mydomain.com中创建的Cookie www子域)?

Can subdomain.mydomain.com access a cookie created in mydomain.com (without the "www" subdomain)?

可以 mydomain.com (不含 www 子网域)如果在 subdomain.mydomain.com

Can mydomain.com (without the www subdomain) access the cookie if created in subdomain.mydomain.com?

推荐答案

2个域 mydomain.com subdomain.mydomain.com 只能共享Cookie,如果域名是明确命名的 Set-Cookie 头。否则,Cookie的范围仅限于请求主机。

The 2 domains mydomain.com and subdomain.mydomain.com can only share cookies if the domain is explicitly named in the Set-Cookie header. Otherwise, the scope of the cookie is restricted to the request host.

例如,如果您从 subdomain.mydomain.com发送以下标头

Set-Cookie: name=value

然后,不会将请求发送到 mydomain.com 。但是,如果您使用以下操作,它将在两个域上都可用:

Then the cookie won't be sent for requests to mydomain.com. However if you use the following, it will be usable on both domains:

Set-Cookie: name=value; domain=mydomain.com

RFC 2109 ,没有前导点的域意味着它不能在子域上使用,并且只有一个前导点( .mydomain.com

In RFC 2109, a domain without a leading dot meant that it could not be used on subdomains, and only a leading dot (.mydomain.com) would allow it to be used across subdomains.

但是,现代浏览器会遵循新的规范 RFC 6265 ,并且将忽略任何前导点,这意味着您可以在子域以及顶级域使用cookie。

However, modern browsers respect the newer specification RFC 6265, and will ignore any leading dot, meaning you can use the cookie on subdomains as well as the top-level domain.

总而言之,如果您从 mydomain.com 中设置了类似上面第二个示例的cookie,则可以通过 subdomain.mydomain.com ,反之亦然。

In summary, if you set a cookie like the second example above from mydomain.com, it would be accessible by subdomain.mydomain.com, and vice versa.

另请参阅: www vs no-www和cookies 此测试脚本

这篇关于在子域和域之间共享Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆