The previous answers are a little outdated.
RFC 6265 was published in 2011, based on the browser consensus at that time. Since then, there has been some complication with public suffix domains. I've written an article explaining the current situation - http://bayou.io/draft/cookie.domain.html
To summarize, rules to follow regarding cookie domain:
The origin domain of a cookie is the domain of the originating request.
If the origin domain is an IP, the cookie's domain attribute must not be set.
If a cookie's domain attribute is not set, the cookie is only applicable to its origin domain.
If a cookie's domain attribute is set,
It can be derived that a cookie is always applicable to its origin domain.
The cookie domain should not have a leading dot, as in .foo.com
- simply use foo.com
As an example,
x.y.z.com
can set a cookie domain to itself or parents - x.y.z.com
, y.z.com
, z.com
. But not com
, which is a public suffix.y.z.com
is applicable to y.z.com
, x.y.z.com
, a.x.y.z.com
etc.Examples of public suffixes - com
, edu
, uk
, co.uk
, blogspot.com
, compute.amazonaws.com