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!

It's very calm over here, why not leave a comment?

Leave a Reply




Akismet verifies all comments posted here to screen for spams. If your post fails, please try again by removing links, suspicious and random words.