Search code examples
phpsessioncookiessubdomainsession-cookies

Can't share PHP sessions between subdomain and main


I want to make a session on a subdomain, and then access it from my main domain. I have read many threads regarding the same problem but none of the answers work for me.

I have a VPS from Dreamhost, and I have sat the following line into phprc on both domains (phprc is added to php.ini, dreamhost way of editing php.ini) session.cookie_domain = ".MAINDOMAIN.com" where .MAINDOMAIN.com is referring to my domain name. This was the working solution here: Sharing SESSION Variables Between Multiple Subdomains

I have then made a php file I call test.php on both login.DOMAIN.com and DOMAIN.com

On login.DOMAIN.com/test.php I have the following code:

session_start();
$_SESSION['test'] = "Works";
print_r($_SESSION);

The output when I navigate to the file:

Array ( [test] => Works )

After visiting that page I Then go to DOMAIN.com/test.php where the code is:

session_start();
print_r($_SESSION);

And the output is:

Array ( )

I have seen other threads like this: Allow PHP sessions to carry over to subdomains with 4 different options to set the php.ini line (Directly in php.ini, in .htaccess, in the script, and finally php-fpm pool configuration) and I have tried them all with the exception of the last one with php-fpm pool configuration

I have also tried to set this line on top of my php files, before session_start:

session_set_cookie_params(0,"/",".MAINDOMAIN.com",FALSE,FALSE);

And this on top of that:

session_name('mysession');

But nothing works

I have also checked with HTTP Header Live for FF which domain the cookie is set for as the answer here: Why can't I pass user sessions between subdomains? and the string of Set-Cookie is:

Set-Cookie: PHPSESSID=9Q%2Cfrhr747fferf4700; path=/

There is no mention of what domain? What am I doing wrong? Any ideas?


Solution

  • It is a limitation on Dremhost managed VPS, that don't allow sharing php sessions between virtual hosts (Subdomains). I have switched to another provider and everything works