Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can subdomain.example.com set a cookie that can be read by example.com?

I simply cannot believe this is quite so hard to determine.

Even having read the RFCs, it's not clear to me if a server at subdomain.example.com can set a cookie that can be read by example.com.

subdomain.example.com can set a cookie whose Domain attribute is .example.com. RFC 2965 seems to explicitly state that such a cookie will not be sent to example.com, but then equally says that if you set Domain=example.com, a dot is prepended, as if you said .example.com. Taken together, this seems to say that if example.com returns sets a cookie with Domain=example.com, it doesn't get that cookie back! That can't be right.

Can anyone clarify what the rules really are?

like image 739
Christophe Avatar asked Jun 21 '10 23:06

Christophe


People also ask

Can I set a cookie for a subdomain?

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 cookies be read by subdomains?

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 be read by example.com and any subdomain, including foo.example.com, baz.bar.foo.example.com, and baz.example.com.

What is the subdomain of example com?

org, and the second-level domain (SLD) is the unique part of the domain name, often a business or brand name. In the hubspot.com example, com is the TLD and hubspot is the SLD. The subdomain is what goes before the SLD. The most common subdomain is www, which stands for World Wide Web.

Are subdomains considered third party cookies?

Conclusion: if a resource sets a cookie and the base domain on the resource is the same as the base domain on the web site, but the subdomain is different, popular browsers do not treat it as a third-party cookie.


1 Answers

Yes.

If you make sure to specify that the domain is .example.com, then *.example.com and example.com can access it.

It's that principal that allows websites that issue cookies when somebody goes to www.website.com to access cookies when someone leaves off the www, going to website.com.

EDIT: From the PHP documentation about cookies:

domain The domain that the cookie is available. To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. The . is not required but makes it compatible with more browsers. Setting it to www.example.com will make the cookie only available in the www subdomain. Refer to tail matching in the » spec for details. http://php.net/manual/en/function.setcookie.php

And it's not unique to PHP.

like image 133
Aaron Yodaiken Avatar answered Sep 20 '22 02:09

Aaron Yodaiken