Mar 17 2008

Persistent Cookies Across Subdomains

Published by Lord TCT at 7:30 pm under PHP

PHP

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!

No responses yet

Trackback URI | Comments RSS

Leave a Reply

Anti-spam challenge:
Sorry but you forced me to do this!