1

i have some questions about the PHP Sessions i couldnd figure out with the pages i found.

But first some general information, i want to create multiple subdomains on one server,

sub1.domain.com --> 10.10.10.10 (Sample IP of the Server)

sub2.domain.com --> 10.10.10.10 (Sample IP of the Server)

sub3.domain.com --> 10.10.10.10 (Sample IP of the Server)

all of this subdomains will work with the same files but they need to have their own sessions, for example if i am logged in on sub1 and i open sub2 i need to be logged out for this subdomain.

  1. Can someone explain me how this may work?
  2. How does this work with multiple servers (round robin dns for example), does all servers know the session of for example sub1?

1 Answer 1

1

By default, PHP uses the 'PHPSESSID' cookie to propagate session data across multiple pages, and by default it uses the current top-level domain and subdomain in the cookie declaration.

Example: www.domain.com

The downside to this is that the session data can't travel with you to other subdomains. So if you started a session on www.domain.com, the session data would become unavailable on forums.domain.com. The solution is to change the domain PHP uses when it sets the 'PHPSESSID' cookie.

Assuming you have an init file that you include at the top of every PHP page, you can use the ini_set() function. Just add this to the top of your init page:

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

This line of code takes the domain and lops off the subdomain.

Example: forums.domain.com -> .domain.com

Now, every time PHP sets the 'PHPSESSID' cookie, the cookie will be available to all subdomains!


you need to

ini_set("session.cookie_domain", ".mydomain.com");

add it before the session.start() function on any page which creates the session cookie.

Or, you can add:

session.cookie_domain = .mydomain.com

to php.ini

Make sure you've cleared your cookies before you try that.

Sign up to request clarification or add additional context in comments.

1 Comment

One more question, when i make a AAA-Record with domain2.com to sub1.domain.com and i open the page over domain2.com, may this solve the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.