PHP sessions & subdomains

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
funkycode
Forum Newbie
Posts: 9
Joined: Fri Dec 12, 2008 8:17 am

PHP sessions & subdomains

Post 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
SteveC
Forum Commoner
Posts: 44
Joined: Thu Dec 04, 2008 2:39 pm
Location: Lansing, MI

Re: PHP sessions & subdomains

Post 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!
funkycode
Forum Newbie
Posts: 9
Joined: Fri Dec 12, 2008 8:17 am

Re: PHP sessions & subdomains

Post 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!
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: PHP sessions & subdomains

Post by Chalks »

check phpinfo() and see if session.auto_start is set to true. If it is, that may be causing some issues.
funkycode
Forum Newbie
Posts: 9
Joined: Fri Dec 12, 2008 8:17 am

Re: PHP sessions & subdomains

Post 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?
funkycode
Forum Newbie
Posts: 9
Joined: Fri Dec 12, 2008 8:17 am

Re: PHP sessions & subdomains

Post 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?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: PHP sessions & subdomains

Post 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!
funkycode
Forum Newbie
Posts: 9
Joined: Fri Dec 12, 2008 8:17 am

Re: PHP sessions & subdomains

Post 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.
funkycode
Forum Newbie
Posts: 9
Joined: Fri Dec 12, 2008 8:17 am

Re: PHP sessions & subdomains

Post 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
syth04
Forum Newbie
Posts: 14
Joined: Thu Dec 18, 2008 12:12 am

Re: PHP sessions & subdomains

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: PHP sessions & subdomains

Post 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?
funkycode
Forum Newbie
Posts: 9
Joined: Fri Dec 12, 2008 8:17 am

Re: PHP sessions & subdomains

Post 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.
funkycode
Forum Newbie
Posts: 9
Joined: Fri Dec 12, 2008 8:17 am

Re: PHP sessions & subdomains

Post 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. :)
fahad_direct
Forum Newbie
Posts: 1
Joined: Sat May 01, 2010 1:20 pm

Re: PHP sessions & subdomains

Post 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
Post Reply