Gramps
Gramps

Reputation: 326

PHP Session fails on sub-domains

So I had a login/session system set up that worked perfectly for a good while. However, within the last few days the sessions fail to carry to the sub-domains.

I have been using the following above the session_start call:

session_set_cookie_params(0, '/', '.domain.com');

Again, this worked fine. You could log in at the base domain then move to a sub-domain and the session carried over. Or you could log in at a sub-domain and move to others fine. Example:

No changes have been made at all to this script. It just fails. Perhaps the host is responsible?

EDIT: Even if you log in on the sub-domain, the PHP session is created but doesn't work. No $_SESSION array data works, etc., though you can see the PHP session in the browser's cookies.

Upvotes: 1

Views: 364

Answers (2)

Gramps
Gramps

Reputation: 326

Fixed it. It seems my session.save_path in PHP.ini was set to /tmp which, for whatever reason, wasn't letting my subdomains work or access the session files.

I changed the PHP.ini to a new folder in the html hierarchy and it worked.

Still using the session_set_cookie_params(0, '/', '.domain.com'); and all my previous settings. So my code was fine, somehow the save path stopped working. Very odd.

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Try using ini_set() function, like:

ini_set('session.cookie_domain',
substr($_SERVER['SERVER_NAME'], strpos($_SERVER['SERVER_NAME'],"."),100) );

Or try using .htaccess

php_value session.cookie_domain .domain.com

OR try setting secure param to false , like

session_set_cookie_params(0, '/', '.domain.com', false, false);

Upvotes: 0

Related Questions