Javascript 无法被子域继承使用的cookie

阅读(2878)

以下两个cookie 有什么区别?

—————————————————————————————————————————————————————————————————
Name    |   Value   |   Domain          |   Path    |   Expires
—————————————————————————————————————————————————————————————————
cookieA |   1       |   kaifage.com     |   /       |   Session
—————————————————————————————————————————————————————————————————
cookieB |   1       |   .kaifage.com    |   /       |   Session
—————————————————————————————————————————————————————————————————

cookieA,domain 没有“.”前缀,此cookie仅在当前域名下有效,其子域默认无法拿到,如www.kaifage.com无法拿到cookieA。
cookieB,domain 有“.”前缀,此cookie可以被子域拿到,如www.kaifage.com可以拿到cookieB。

如果HTTP Header里的Set-Cookie 不指定domain,则在浏览器下生效的是:默认为不带“.”的当前域名。

以上,为chrome实际测试结果。

但是,根据最新 RFC 6265 的规定,不应该有此区别。

For example, if the value of the Domain attribute is
"example.com", the user agent will include the cookie in the Cookie
header when making HTTP requests to example.com, www.example.com, and
www.corp.example.com. (Note that a leading %x2E ("."), if present,
is ignored even though that character is not permitted, but a
trailing %x2E ("."), if present, will cause the user agent to ignore
the attribute.)

Cookie 几个版本的进化史(拟定后者淘汰前者):

  1. rfc2109 : http://tools.ietf.org/html/rfc2109
  2. rfc2965 : http://tools.ietf.org/html/rfc2965
  3. rfc6265 : http://tools.ietf.org/html/rfc6265

好吧,此文标题是个伪命题。

Tags: