PHP sessions across subdomains , the multiple servers issue!
Ok so sometimes we need to carry the sessions across all of the subdomains. So this is one thing you can do in your .htaccess file.
php_value session.cookie_domain “.domain.com”
Incase of cookies you most probably know what to do but as a reminder here it is.
setcookie(”TestCookie”, $value, time()+3600, “/~rasmus/”, “.example.com”, 1);
Notice the .domain.com in both cases , this lets the cookies be accessable from all subdomains.
Now thats all goody goody BUT when it comes to having a domain span across multiple servers , like lets say you host the main domain.com on server 1 and the subdomain abc.domain.com on server 2, the session id will be carried across just fine if you use the .htaccess method BUT whatever is inside the session will NOT be available across both servers. The reason is pretty simple , lets say you begin your session on server 1 , you store some data in the session and it is stored in the domain.com session save directory BUT when you goto abc.domain.com , you cannt access the data stored in the session save directory from server 1. So basically this doesnt help much across multiple servers, does it?
Ok so what can we do about this?
Now after some thinking a probable solution was to use the database and keep the session data in there. But i didnt like the extra load it was putting on the MySql server.
So after alot of thinking I devised a little plan for this , now there is one thing we do have as common on both ends , we have the session id , now you can integrate a crypt / decrypt mechanism with the session id acting as the key, to solidify it further , add some more characters to the session id and use that as a key. Then send the session data in the query string.
something like this:
$key = “somethinghere”.session_id() . “somethingmore”;
Use the above generated key on server 1 to generate the query string and decode it with the same key on server 2 and vice versa if need be.

January 3rd, 2007 at 10:02 am
Hi,
i am a PHP programmer, I have problem simililar to this post but a little different.
I have two sites on same server say http://www.abc.com and http://www.xyz.com, I want to set cookie or session on http://www.abc.com which can be accessed from http://www.xyz.com, is this possible ?
Regards
Anup Singh
January 31st, 2007 at 1:26 pm
I managed to create a not very invasive solution to this problem.
Its somewhat advanced in the sense it involves mod-rewrite and php image manipulation. The way I did it was to use images.
For example http://something.com/something/spacer.jpg
gets mod-rewrite to a script set_cookies.php?perv_id=1234&scroll=left&right_scroll=right&mascot_header=no
then when you throw this up in an it will set the cookies on pussybum from beaverbum.
set_cookies.php sets the cookeis then pulls a 1×1 white image and displays it. So to set 9 cookies i end up with a little 9 pixel spot on my page. i have this working on the same server but it should work across multiple servers too since the variables get passed through GET