Exchanging $ _SESSION Variables Across Subdomains Using PHP
I'm trying to share the content of a session variable across two subdomains, but for some reason it doesn't work.
sessionid in both subdomains is the same, but the variables are not available.
I can achieve this with Cookies and it works, but would rather use the values in the session.
This is how I set the domain for the session:
Thanks Scott
UPDATE Sorry, had to say that I am already using the following:
ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
if(session_id ==''){session_start();}
a source to share
Are you looking for this function: session_set_cookie_params()
. Let's say you have two domains:
- site1.example.com
- site2.example.com
To share your session data across both domains, call the following function before session_start()
:
session_set_cookie_params(0, '/', '.example.com');
(Edit: indeed, I forgot the point in the example code)
a source to share
Sessionid in the same way for both subdomains (..)
Are you sure the value (not the name) of the sessionid is the same?
I needed a similar thing 2 years ago, but I needed to work on completely different virtual hosts (like example.com and something.else.net), so I wrote a small script that makes sure the user gets the same ones session_id
on different virtual hosts. The source code is available at http://www.misc.lv/_downloads_file.php?id=18 , but a small mistake on line 58 - the strpos () arguments must be replaced (I'm also lazy to fix it and load the fixed script).
Basic requirements / steps:
- all pages must be on the same server, as they require a common "storage";
- there must be one "main" owner; there is no limit on the number of "additional" hosts;
- If the currently open page is on the "primary host" (and this is not a session_id request), nothing unusual should be done;
- If the page is not on the "master host" and there is an active session, nothing unusual needs to be done, since the session_id may have already been shared;
- If the page is not on the "main host" and there is no active session, request the session_id from the "main host".
a source to share