PHP session with subdomain

I read a lot of forums (including this one) about passing session variables between subdomains, and I can't get this to work. Can someone explain what I'm missing?

Step 1 In the php.ini file: session.cookie_domain = ".mydomain.com" Checked with phpinfo () that I am using the correct php.ini file

Step 2 On the page www.mydomain.com set the session variable $ _SESSION ['a'], make sure that it is displayed by calling it on the next page (it does). Clink Link to sub.mydomain.com

Step 3 The page at sub.mydomain.com checks to see if the session variable is set using:

$ a = $ _SESSION ['a']; if (! Iset ($ _ SESSION ['a'])) {echo "Error: session variable unavailable"; }

Sorry, I am getting an error. What am I missing? Thanks in advance for your help.

+1
php subdomain session
May 14, '10 at 15:45
source share
3 answers

So, I went in a different direction and used this entry, which worked ...

session_set_cookie_params (0, '/', '.mydomain.com'); session_start ();

+3
May 14, '10 at 16:09
source share

You must pass the session id as a cookie and set the same session id in the new domain

For example, you can use this code

ini_set('session.cookie_domain', '.example.com'); $currentCookieParams = session_get_cookie_params(); $rootDomain = '.example.com'; session_set_cookie_params( $currentCookieParams["lifetime"], $currentCookieParams["path"], $rootDomain, $currentCookieParams["secure"], $currentCookieParams["httponly"] ); if(!empty($_SESSION)){ $cookieName = session_id(); setcookie('PHPSESSID', $cookieName, time() + 3600, '/', $rootDomain); } if(isset($_COOKIE['PHPSESSID'])){ session_name($_COOKIE['PHPSESSID']); } 
+3
Jul 14 '13 at 9:21
source share

debugging.
this is what you are missing.

First of all, you need to look at the HTTP headers to see what happens and which cookies are actually set. You can use the Firefox LiveHTTPHeaders addon or something else. With such information you may find a problem. Without it, no one can answer the question of the tour: "My sessions do not work"

He can confirm your statement about the correct domain settings in the session settings. Or disprove it.
This may show some other incorrect settings.
This can show you that the cookie is being sent back by the browser - so you can be sure that this is a server-side issue.

Always helps <actual result your code (instead of guessing based on indirect consequences).

+2
May 14 '10 at 15:57
source share



All Articles