How to share cookies across multiple Apache Virtual Hosts
- by puk
This question is generally phrased as "How to share cookies across multiple subdomains" and the answer is generally to use the leading dot like so
setcookie( 'id', 0, time()+30*3600, '/', '.example.com' );
which means that the cookie is available to all subdomains of example.com. However, the / path I suspect adds the constraint that all subdomains must be physically under the same tree. The PHP documentation states
path
The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.
Is it possible to share cookies if one has two (Apache) Virtual Hosts set up with document roots at, for example
www.one.example.com ? /var/www/example1
www.two.example.com ? /var/www/example2