来自RFC 6265:
5.1.3. Domain Matching
A string domain-matches a given domain string if at least one of the
following conditions hold:
o The domain string and the string are identical. (Note that both
the domain string and the string will have been canonicalized to
lower case at this point.)
o All of the following conditions hold:
* The domain string is a suffix of the string.
* The last character of the string that is not included in the
domain string is a %x2E (".") character.
* The string is a host name (i.e., not an IP address).
也就是说,如果 cookie 的 domain 参数中的域名不以句点开头,那么它不会让子域读取该 cookie。如果它确实以句点开头,那么所有子域都可以完全访问该 cookie 的值。
例如:
Set-Cookie: lang=en-US; Path=/; Domain=example.com
只能由 example.com 阅读。
Set-Cookie: lang=en-US; Path=/; Domain=.example.com
可以被 example.com 和任何子域读取,包括 foo.example.com、baz.bar.foo.example.com 和 baz.example.com。
Set-Cookie: lang=en-US; Path=/; Domain=foo.example.com
只能由 foo.example.com 读取。
Set-Cookie: lang=en-US; Path=/; Domain=.foo.example.com
可以被 foo.example.com 和任何子域读取,包括 baz.bar.foo.example.com,但不能被 example.com 或 baz.example.com 读取。
如果您想跨子域共享 cookie,但忽略其他子域,则应明确说明要读取哪些子域,为每个子域设置一个新 cookie,而不是使用通配符。