About Cookie
请求返回 set-cookie 的域名是如何生效的
cookie 是在 response 的 header 中的 set-cookie
字段,告诉浏览器在对应的域名下存储 cookie
比如:
store-xxx=abc; Path=/; Domain=xxx.com; Max-Age=31536000; HttpOnly
对于域名的处理,According to the RFC 2965, the following should apply:
- If the Set-Cookie header field does not have a Domain attribute, the effective domain is the domain of the request.
- If there is a Domain attribute present, its value will be used as effective domain (if the value does not start with a
.
it will be added by the client).
意思就是两种情况:
- 没带 Domain 字段的,会将请求的域名作为 effective domain(存 cookie)
- 带了 Domain 字段就用这个值,并会进行一些处理
- Cookie with
Domain=.example.com
will be available forwww.example.com
orabc.example.com
- Cookie with
Domain=.example.com
will be available forexample.com
- Cookie with
Domain=example.com
will be converted to.example.com
and thus will also be available forwww.example.com
- Cookie with
Domain=example.com
will not be available foranotherexample.com
- Cookie with
set cookie 是如此,read cookie 也遵循这个规则