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

Can access a cookie created in (without the www subdomain)?

Can (without the www subdomain) access the cookie if created in

6/13/2017 9:04:12 PM

Accepted Answer

The 2 domains and 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. (This is referred to as a "host-only cookie". See What is a host only cookie?)

For instance, if you sent the following header from, then the cookie won't be sent for requests to

Set-Cookie: name=value

However if you use the following, it will be usable on both domains:

Set-Cookie: name=value;

This cookie will be sent for any subdomain of, including nested subdomains like

In RFC 2109, a domain without a leading dot meant that it could not be used on subdomains, and only a leading dot ( would allow it to be used across multiple subdomains (but not the top-level domain, so what you ask was not possible in the older spec).

However, all 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.

In summary, if you set a cookie like the second example above from, it would be accessible by, and vice versa. This can also be used to allow and to share cookies.

See also:

8/30/2019 7:04:14 AM

I'm not sure @cmbuckley answer is showing the full picture. What I read is:

Unless the cookie's attributes indicate otherwise, the cookie is returned only to the origin server (and not, for example, to any subdomains), and it expires at the end of the current session (as defined by the user agent). User agents ignore unrecognized cookie.

RFC 6265


8.6.  Weak Integrity

   Cookies do not provide integrity guarantees for sibling domains (and
   their subdomains).  For example, consider and  The server can set a cookie with a
   Domain attribute of "" (possibly overwriting an existing
   "" cookie set by, and the user agent will
   include that cookie in HTTP requests to  In the
   worst case, will be unable to distinguish this cookie
   from a cookie it set itself.  The server might be
   able to leverage this ability to mount an attack against

To me that means you can protect cookies from being read by subdomain/domain but cannot prevent writing cookies to the other domains. So somebody may rewrite your site cookies by controlling another subdomain visited by the same browser. Which might not be a big concern.

Awesome cookies test site provided by @cmbuckley /for those that missed it in his answer like me; worth scrolling up and upvoting/:


Here is an example using the DOM cookie API (, so we can see for ourselves the behavior.

If we execute the following JavaScript:

document.cookie = "key=value"

It appears to be the same as executing:

document.cookie = "key=value;"

The cookie key becomes available (only) on the domain

Now, if you execute the following JavaScript on

document.cookie = "key=value;"

The cookie key becomes available to as well as

Finally, if you were to try and execute the following on

document.cookie = "key=value;"

Does the cookie key become available to I was a bit surprised that this is allowed; I had assumed it would be a security violation for a subdomain to be able to set a cookie on a parent domain.


Please everyone note that you can set a cookie from a subdomain on a domain.

(sent in the response for requesting

Set-Cookie: name=value; // GOOD

But you CAN'T set a cookie from a domain on a subdomain.

(sent in the response for requesting

Set-Cookie: name=value; // Browser rejects cookie


According to the specifications RFC 6265 section 5.3.6 Storage Model

If the canonicalized request-host does not domain-match the domain-attribute: Ignore the cookie entirely and abort these steps.

and RFC 6265 section 5.1.3 Domain Matching

Domain Matching

A string domain-matches a given domain string if at least one of the following conditions hold:

  1. 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.)

  2. 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).

So "" domain-matches "", but "" does NOT domain-match ""

Check this answer also.


In both cases yes it can, and this is the default behaviour for both IE and Edge.

The other answers add valuable insight but chiefly describe the behaviour in Chrome. it's important to note that the behaviour is completely different in IE. CMBuckley's very helpful test script demonstrates that in (say) Chrome, the cookies are not shared between root and subdomains when no domain is specified. However the same test in IE shows that they are shared. This IE case is closer to the take-home description in CMBuckley's www-or-not-www link. I know this to be the case because we have a system that used different servicestack cookies on both the root and subdomain. It all worked fine until someone accessed it in IE and the two systems fought over whose session cookie would win until we blew up the cache.


Be careful if you are working on localhost ! If you store your cookie in js like this:

document.cookie = "key=value;domain=localhost"

It might not be accessible to your subdomain, like sub.localhost. In order to solve this issue you need to use Virtual Host. For exemple you can configure your virtual host with ServerName then you will be able to store your cookie on your domain and subdomain like this:

document.cookie = "key=value;"