Allow PHP sessions to carry over to subdomains

Viewed 79218

I use PHP sessions (not cookies, except for session id cookie) for all user data, and when a user goes to their profile user.mydomain.example they are immediately "logged out" until then remove the subdomain.

Is there a way to accept sessions from all domains as long as its *.mydomain.example

10 Answers

Here are 4 options.

Place this in your php.ini:

session.cookie_domain = ".example.com"

Or in your .htaccess:

php_value session.cookie_domain .example.com

Or as the first thing in your script:

ini_set('session.cookie_domain', '.example.com' );

Or in your php-fpm pool configuration for your site:

php_value[session.cookie_domain] = .example.com

Before session_start() use session_set_cookie_params() replacing .domain.example with your domain like this example:

session_set_cookie_params(0, '/', '.domain.example');
session_start();
Related