Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

Can mydomain.com (without the www subdomain) access the cookie if created in subdomain.mydomain.com?

like image 478
adam0101 Avatar asked Aug 28 '13 15:08

adam0101


People also ask

Can cookie be shared across subdomains?

Please everyone note that you can set a cookie from a subdomain on a domain. But you CAN'T set a cookie from a domain on a subdomain.

Can subdomain read cookie?

That is, if the domain name in your cookie's domain parameter doesn't start with a period, then it will not let subdomains read that cookie. If it does start with the period, then all subdomains will have full access to that cookie's value. Can only be read by example.com.

Can cookies be shared between domains?

To share a cookie between domains, you will need two domains, for example myserver.com and slave.com . One of the domains will issue the cookies and the other domain will ask the first domain what cookie should be issued to the client.


1 Answers

Two different domains (e.g. mydomain.com and subdomain.mydomain.com, or sub1.mydomain.com and sub2.mydomain.com) 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 subdomain.mydomain.com, then the cookie would only be sent for requests to that domain, and won't be sent for requests to any other domains:

Set-Cookie: name=value 

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

Set-Cookie: name=value; domain=mydomain.com 

The domain attribute must "domain-match" the domain in the URL for it to be valid, i.e. the attribute must be the same domain or a super-domain.

The cookie above would then be sent for any subdomain of mydomain.com, including nested subdomains like subsub.subdomain.mydomain.com.


In RFC 2109, a domain without a leading dot meant that it could not be used on subdomains, and only a leading dot (.mydomain.com) 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 mydomain.com, it would be accessible by subdomain.mydomain.com, and vice versa. This can also be used to allow sub1.mydomain.com and sub2.mydomain.com to share cookies.

See also:

  • www vs no-www and cookies
  • cookies test script to try it out
like image 55
cmbuckley Avatar answered Sep 28 '22 03:09

cmbuckley