Mar 17 2008
Persistent Cookies Across Subdomains
One of the most common problems encountered by novice PHP users is that cookies do not seem to persistent across subdomains using a normal approach found in many website examples.
The solution is simple. The full argument of PHP’s setcookie function is as follow:
bool setcookie ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly ]]]]]] )
The trick is to set the value of $domain, something almost all self help tutorials will omit.
For example, to set a particular cookie to be accessible on all subdomains under bitsofbytes.org, we will set $domain = “.bitsofbytes.org”.
Simple as that!