Page 1 of 1

PHP sessions & subdomains

Posted: Fri Dec 12, 2008 8:41 am
by funkycode
Hi everyone,

My first post here, but I've been reading thread on here for quite a while now. I've run into a stupid problem which is probably quite simple to solve but so far I've had no luck with it (been searching all day :( ).

Basically I'm having trouble with sessions across subdomains on the live site (it works fine on my dev server). I cannot get the sessions to stick across sub domains whatever I try!

I even created a separate simplified test to see if it was my actual script causing the problem but apparently it's not.

Here's the code I've tested successfully on my dev server which won't work on the production one:
http://www.local.lh/startSession.php: (I've set this to replace the usual http://localhost)

Code: Select all

<?php
session_set_cookie_params(3600, '/', '.local.lh');
 
session_start();
 
$_SESSION['test'] = 'Working?';
 
?>
And
http://sub.local.lh/showSession.php:

Code: Select all

<?php

session_start();

print_r($_SESSION);
 
?>
This works perfectly on localhost but not on the website (yes I did replace .local.lh with .mydomain.com :) )

I've checked all the session settings with phpinfo(); and the only differences are:
  • session.gc_probability 1 on server (0 on local)
  • session.save_path no value on server (/var/lib/php5 on local)
I've adjusted the settings of the sessions on my localhost to match those on the server but it still works whereas it doesn't on the live server. Both are running PHP 5.2.6

I'd really appreciate some help if anyone has any ideas. If you need more details just ask!

Thanks in advance,
Ben

Re: PHP sessions & subdomains

Posted: Fri Dec 12, 2008 1:59 pm
by SteveC
Have you tried clearing your cookies and then testing? Everytime I have problems with cookies, it seems to be remnants of old cookies causing the problems!

Re: PHP sessions & subdomains

Posted: Fri Dec 12, 2008 6:16 pm
by funkycode
Hi Steve, thanks for the idea but unfortunately I've already tried this.

It solved the problem when I was testing it locally but didn't make a difference on the main website.

The code I posted above is definitely the correct way of doing it right? Albeit in a much simpler context then how it would normally be used.

Anyway I've given up for tonight, hopefully tomorrow the solution will appear or it'll just start working (one can always hope :lol: )

Thanks & any more ideas are welcome!

Re: PHP sessions & subdomains

Posted: Fri Dec 12, 2008 11:36 pm
by Chalks
check phpinfo() and see if session.auto_start is set to true. If it is, that may be causing some issues.

Re: PHP sessions & subdomains

Posted: Mon Dec 15, 2008 4:05 am
by funkycode
Nope, it's set to 'off' on both local & server.

The session is definitely being created. When I view cookies through web dev toolbar in FF I get:

Code: Select all

Name    PHPSESSID
Value   55aabaec7eb420aad5174cf7193e71e7
Host    .domain.com
Path    /
Secure  No
Expires Mon, 15 Dec 2008 10:59:29 GMT
Viewing the cookies from the subdomain also returns exactly the same. But php won't read the session?

Re: PHP sessions & subdomains

Posted: Mon Dec 15, 2008 6:45 am
by funkycode
I've just tested this on another server & it works fine. What settings server-side could cause this problem? Could it be Apache's settings?

Re: PHP sessions & subdomains

Posted: Mon Dec 15, 2008 8:35 am
by shiznatix
I had the same problem the other day and found the solution. Basically you have to force using session cookies. in your php.ini file add this:

session.use_cookies = 1
session.use_only_cookies = 1
session.cookie_domain = .yourdomain.com

then when you set a cookie you have to do it like this:

setcookie($name, $data, $lastingTime, '/', '.yourdomain.com');

this should solve your problem, did for me.

edit: make sure you notice the . (period) before the yourdomain.com!

Re: PHP sessions & subdomains

Posted: Mon Dec 15, 2008 9:24 am
by funkycode
Thank you, thank you, thank you. :D

That's solved it, it was the use_only_cookies which was causing the problem. I don't quite understand why it works locally with it set on 0 & not on the server but the main thing is that it works. :)

Thanks again you've saved me from going mad.

Re: PHP sessions & subdomains

Posted: Thu Dec 18, 2008 7:43 am
by funkycode
I don't believe this... Everything was working fine, I just updated PHP on the server from 5.2.6 to 5.2.8 and it stopped working.. :banghead:

I've checked the phpinfo() and nothing has changed within the session settings. Does my server hate me or is there something in 5.2.8 which could have caused this or am I just being stupid & missing something dead obvious (the most likely cause :D )? I looked through the PHP changelog and couldn't see anything that would affect it.

Thanks in advance for any help or suggestions you may have!

Ben

Re: PHP sessions & subdomains

Posted: Thu Dec 18, 2008 2:44 pm
by syth04
funkycode wrote:I don't believe this... Everything was working fine, I just updated PHP on the server from 5.2.6 to 5.2.8 and it stopped working.. :banghead:

I've checked the phpinfo() and nothing has changed within the session settings. Does my server hate me or is there something in 5.2.8 which could have caused this or am I just being stupid & missing something dead obvious (the most likely cause :D )? I looked through the PHP changelog and couldn't see anything that would affect it.

Thanks in advance for any help or suggestions you may have!

Ben

Expire the cookies, then try it again. May be thinking its still valid for any session.

Re: PHP sessions & subdomains

Posted: Thu Dec 18, 2008 2:48 pm
by RobertGonzalez
5.2.8 is pretty new. Is your local copy 5.2.8 as well? Also, is your local server the same OS as your production server?

Re: PHP sessions & subdomains

Posted: Fri Dec 19, 2008 3:11 am
by funkycode
syth04, thanks but I've been trying this every time.

Everah, the local is 5.2.6 on Ubuntu & the server is CentOs 5 with PHP 5.2.8

I'll upgrade to 5.2.8 on my local server & see if that breaks it locally.

Re: PHP sessions & subdomains

Posted: Sat Dec 20, 2008 8:56 am
by funkycode
No luck solving it so far, we are moving to a new dedicated server over the next week so I'll see if it works on the new one. :)

Re: PHP sessions & subdomains

Posted: Sat May 01, 2010 1:33 pm
by fahad_direct
Hi, I have an issue and unable to get the solution if anyone can help me please:
I am using mod rewrite for my urls and having the problem in sessions as session is losing its value across the urls as already doing as:

php_value session.cookie_domain ".domain.com"
in .htaccess file

I am doing mode rewrite so even it is working fine there and i can share the same session from one subdirectory to another even on root but it is being losed if i am accessing from one more inner folder inside abc subdirectory.

It works fine only upto the levels of subdirectories but if there are further folders inside, sessions are being losed