Psyche
Psyche

Reputation: 8773

Keeping session information between a primary domain and a subdomain

I was wondering if it is possible to retain session information between a primary domain and a subdomain. For example, I login on http://www.mydomain.com/ but I want to use the same session on http://something.mydomain.com/.

Upvotes: 0

Views: 1521

Answers (2)

Gumbo
Gumbo

Reputation: 655319

Yes, if the session data is accessible from both domains (e.g. same server) and if the same session ID is used for both domains, you can share the same session in both domains.

As for the session ID, if you use a cookie for the session ID, you need to adjust the session cookie’s domain parameter to make the cookie available on both domains (i.e. .example.com for example.com and all its subdomains):

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

Upvotes: 0

Mano Kovacs
Mano Kovacs

Reputation: 1514

Use session_set_cookie_params()

session_set_cookie_params ( $lifetime , '/', '.yourdomain.com');

Note, that the domain starts with a dot. This means, the session cookie is valid for every subdomain!

http://php.net/manual/en/function.session-set-cookie-params.php

Upvotes: 3

Related Questions