Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing SESSION Variables Between Multiple Subdomains

I have a website www.example.com. That will have multiple subdomains that work with a single application or program. For an example, login.example.com will allow the user to log in to the site while system.example.com will allow the user to access an information system, while forums.example.com will allow the user to access forums.

We may need to pass information between the subdomains such as a user id, or a user preference, etc. How do we go about passing information between the sudomains using SESSION variables?

EDIT: I like this idea:

As the first thing in your script:

ini_set('session.cookie_domain', '.example.com' ); 
like image 473
Kevin Oluseun Karimu Avatar asked Feb 05 '12 22:02

Kevin Oluseun Karimu


People also ask

Can you have 2 subdomains?

You create subdomains to help organize and navigate to different sections of your main website. Within your main domain, you can have as many subdomains as necessary to get to all of the different pages of your website.

Can you stack subdomains?

Since subdomains effectively replace the domain and count as that domain for pretty much all purposes, and you can't take a subdomain and its domain together, you can't take both.

Can users see SESSION variables?

No, SESSION variables are on the server side so from the client's perspective, they cannot change them. That's one of the main reasons we use Sessions instead of cookies.

Can two subdomains share cookies?

To share cookies across subdomains, you can simply create cookies with the domain directive set to the parent domain, in this case, example.com, rather than either of the specific subdomains. In my browser, I have the index and product detail pages open.


1 Answers

1) the subdomains should use the same path to save session files

2) modify your

php.ini session.cookie_domain = ".example.com"

or .htaccess php_value session.cookie_domain .example.com

or inside of the script ini_set('session.cookie_domain', '.example.com' );

like image 167
Cheery Avatar answered Oct 09 '22 10:10

Cheery