If you’re using setcookie
and do not specify a domain in the domain parameter, setcookie
will not set the corresponding Domain parameter in the Set-Cookie header field (this also applies to session.cookie_domain).
Now if no Domain parameter is present, the user agent defaults the domain value to the to the request-host (see RFC 2109 section 4.3.1):
The user agent applies these defaults for optional attributes that are missing:
- Domain – Defaults to the request-host. (Note that there is no dot at the beginning of request-host.)
If there is a Domain parameter present, its value must begin with a dot, otherwise the user agent will reject the cookie (see RFC 2109 section 4.3.2):
To prevent possible security or privacy violations, a user agent rejects a cookie (shall not store its information) if any of the following is true:
- The value for the Domain attribute contains no embedded dots or does not start with a dot.
So, as also described on the setcookie
manual page, use .example.com
if you want to make your cookie available on example.com and its subdomains, and do not specify a domain if you only want to make it available on the current domain.