本文翻译自:Share cookie between subdomain and domain
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. 我理解,如果我在cookie中将域指定为.mydomain.com
(带有前导点),则所有子域都可以共享cookie。
Can subdomain.mydomain.com
access a cookie created in mydomain.com
(without the www
subdomain)? subdomain.mydomain.com
可以访问在mydomain.com
创建的cookie(没有www
子域)吗?
Can mydomain.com
(without the www
subdomain) access the cookie if created in subdomain.mydomain.com
? 如果在subdomain.mydomain.com
创建, mydomain.com
(没有www
子域)可以访问cookie吗?
#1楼
参考:https://stackoom.com/question/1FalM/在子域和域之间共享cookie
#2楼
The 2 domains mydomain.com
and subdomain.mydomain.com
can only share cookies if the domain is explicitly named in the Set-Cookie
header. 如果在Set-Cookie
标头中明确命名域,则2个域mydomain.com
和subdomain.mydomain.com
只能共享cookie。 Otherwise, the scope of the cookie is restricted to the request host. 否则,cookie的范围仅限于请求主机。 (This is referred to as a "host-only cookie". See What is a host only cookie? ) (这被称为“仅限主机的cookie”。请参阅什么是仅限主机的cookie? )
For instance, if you sent the following header from subdomain.mydomain.com
, then the cookie won't be sent for requests to mydomain.com
: 例如,如果您从subdomain.mydomain.com
发送以下标头,则不会将请求发送到mydomain.com
:
Set-Cookie: name=value