Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow php sessions to carry over to subdomains

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.com they are immediately "logged out" untill then remove the subdomain.

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

like image 262
Anthony Avatar asked Mar 13 '09 23:03

Anthony


People also ask

How do I share a session between two subdomains?

since separate websites don't share sessions (as far as i know, since subdomains are technically "different places" from eachother), don't use sessions to store on the server side. instead, use a database to handle your sessions. that way, multiple sites can share the same session tracking table.

What is PHP Session_start () function?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

Are cookies available across subdomains?

Please everyone note that you can set a cookie from a subdomain on a domain. But you CAN'T set a cookie from a domain on a subdomain.

Can you stack subdomains?

Yes. You can have as many levels as you like in DNS.


1 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 
like image 181
CTT Avatar answered Sep 22 '22 15:09

CTT