Page 1 of 1
Sessions not working with www. sub domain.
Posted: Mon Nov 05, 2007 5:35 pm
by JellyFish
Hey, whenever I go to my web site without the www. sub-domain(i.e.
http://mywebsite.com) and log in then go to my web site with the www. sub-domain(i.e.
http://www.mywebsite.com) I'm no longer logged in. I guess session variables don't work across sub-domains? How can I get session variables to work across sub-domains?
That's pretty much it. Thanks!
Posted: Mon Nov 05, 2007 5:56 pm
by feyd
Set the cookie parameters properly.
Posted: Mon Nov 05, 2007 6:32 pm
by JellyFish
feyd wrote:Set the cookie parameters properly.
Could you explain more. What/where are cookie parameters?
Posted: Mon Nov 05, 2007 6:44 pm
by feyd
Look in the session functions. There's only one that deals with cookies.
Posted: Mon Nov 05, 2007 6:48 pm
by JellyFish
feyd wrote:Look in the session functions. There's only one that deals with cookies.
I think I found it. Is it session_set_cookie_params()? If so, then I have some question about it.
php.net says:
void session_set_cookie_params ( int $lifetime [, string $path [, string $domain [, bool $secure [, bool $httponly]]]] )
What if I don't want an lifetime set, nor a path? I'd just like to set the domain to mywebsites.com cause, hopefully, that would solve all my problems.
Posted: Wed Nov 07, 2007 2:16 am
by JellyFish
I'm sorry to say this but, I don't think you're helping much Feyd... Just being honest.

Posted: Wed Nov 07, 2007 4:09 am
by Chris Corbyn
~feyd's just trying to encourage you to use the PHP Manual
Hint (right from php.ini):
Code: Select all
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
Posted: Wed Nov 07, 2007 3:02 pm
by JellyFish
Chris Corbyn wrote:~feyd's just trying to encourage you to use the PHP Manual
Hint (right from php.ini):
Code: Select all
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
Okay I did a little research. Sense I do not recognize, for example, session.cookie_path to be a php object(because I assume all object to start with the $ character), I found that this would be for a .htaccess file?
So, the only way to set cookie parameters with php is through the session_set_cookie_params() function. The only problem with this is: I don't know much about the function as
php.net doesn't explain as much about as I hoped.
So my questions about this function are: What argument should I pass for lifetime? I want the same lifetime that I get by default. Same with the path parameter, what should I pass it; what's the default; "/"?
Once I've had these questions answered then I'd make my way to that so, so special parameter("domain") and my problem would be solved, I'd die a happy adolescent.
PS: The only other option is editing an .htaccess file, but I'd might prefer doing it with php.
Posted: Wed Nov 07, 2007 3:37 pm
by Mightywayne
Set it to how many seconds you want it to be alive for. I personally do 1500 seconds; 25 minutes.
Not that it matters any, but I do suggest you choose the .htaccess option. www's so ugly.
Also, feyd seems to dodge around the answer a lot. Encouraging is nice, but so is the answer.

Posted: Wed Nov 07, 2007 5:58 pm
by JellyFish
Mightywayne wrote:Set it to how many seconds you want it to be alive for. I personally do 1500 seconds; 25 minutes.
Not that it matters any, but I do suggest you choose the .htaccess option. www's so ugly.
Also, feyd seems to dodge around the answer a lot. Encouraging is nice, but so is the answer.

I don't want the sessions for my users to timeout. Would setting it to 1500 seconds make it time out in 25 hours?
Other then that, I did some more searching(it's not like I don't use the php manual). I found ini_set() function. But the problem is, when I write this before I call session_start():
Code: Select all
ini_set("session.cookie_domain", ".mydomain.com");
session_start();
there seems to be no effect. I don't know if ini_set would equate to the same results as session_set_cookie_params().
Also adding to my .htaccess file:
Code: Select all
AddType x-mapp-php5 .php
AddType x-mapp-php5 .php4
php_value session.cookie_domain ".mydomain.com"
does effect my page, but it gives me a server error. Is it because I'm setting the variable wrong: ".mydomain.com" should it be "mydomain.com" or something?
Re: Sessions not working with www. sub domain.
Posted: Wed Jul 30, 2008 12:00 am
by JellyFish
Sorry for bringing back such an old post, but it never had been resolved.
So I believed I learned a lot since when I first started this post so I'm going to give it another shot.
I set the coookie parameters like so:
Code: Select all
session_set_cookie_param(0, "/", "mydomain.com");
So I did this and it doesn't seem to be having the effect I would like. It's not allow all sub-domains to share the same session variables(what I mean by session variables is the $_SESSION array).
What am I doing wrong?
Re: Sessions not working with www. sub domain.
Posted: Wed Jul 30, 2008 3:18 am
by Phoenixheart
Having both
http://www.domain.com and
http://domain.com working at the same time is not a good idea imho anyway. I always choose to permanently redirect from
http://www.domain.com to
http://domain.com (or vice-versa).
Re: Sessions not working with www. sub domain.
Posted: Wed Aug 06, 2008 10:03 pm
by JellyFish
Okay, that's what I had to, eventually, end up doing.
Thanks.
Re: Sessions not working with www. sub domain.
Posted: Sun Apr 05, 2009 12:36 pm
by symmetric
I set the coookie parameters like so:
session_set_cookie_param(0, "/", "mydomain.com");
... It's not allow all sub-domains to share the same session variables...
From the manual:
Cookie domain, for example '
www.php.net'. To make cookies visible on all subdomains then the domain must be prefixed with a dot like '.php.net'.
The "/" makes cookies apply all to paths in the domain, but you need the "." to apply to all subdomains