View Full Version : Sessions Across Subdomains

06-17-2006, 07:18 AM
I've been racking my brain with this and searching all over the internets and haven't been able to find a solution that I've been able to make work. I'm using the awesome login script for evolt.org. Works really well. However I've set up a subdomain with mod_writes on my site. So users have addresses - like http://my.website.com/user

The script is using cookies and sessions and I need the sessions to carry across the the subdomains. However they are not and I can't get them to work. I've read up on several ways to fix the problem but none have worked. So I thought perhaps if I provided the script pieces here that maybe someone could help me.

In the constants.php file:

* Cookie Constants - these are the parameters
* to the setcookie function call, change them
* if necessary to fit your website. If you need
* help, visit www.php.net for more info.
* <http://www.php.net/manual/en/function.setcookie.php>
define("COOKIE_EXPIRE", 60*60*24*100); //100 days by default
define("COOKIE_PATH", "/"); //Avaible in whole domain

Then in the session.php file it sets the cookie here:

* This is the cool part: the user has requested that we remember that
* he's logged in, so we set two cookies. One to hold his username,
* and one to hold his random value userid. It expires by the time
* specified in constants.php. Now, next time he comes to our site, we will
* log him in automatically, but only if he didn't log out before he left.
setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
setcookie("cookid", $this->userid, time()+COOKIE_EXPIRE, COOKIE_PATH);
/* Login completed successfully */
return true;

I tried changing the "/" to ".domain.com" like I saw suggested at several sites but I was still loosing my session at the subdomain.

Any help on this matter is appreciated. I know there's a solution. SOMEWHERE!