hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : [PHP] Sessions over multiple subdomains
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

[PHP] Sessions over multiple subdomains

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 07-16-2008, 05:31 AM
ResellerPlanet ResellerPlanet is offline
Aspiring Evangelist
 
Join Date: Aug 2004
Posts: 414

[PHP] Sessions over multiple subdomains


Hello,

I'm trying to share login information (stored in sessions) between 2 subdomains, ie: sub1.example.com and sub2.example.com. Both subdomains are hosted on the same server.

I've tried several things (like using session_set_cookie_params() to set the cookie domain) and just using a custom php.ini, both to set the cookie domain of the session cookie to ".example.com".

On sub1.example.com I have a test script which sets a session (and displays it once it's been set on each page reload). This works fine. I get:

Quote:
PHPSESSID c6425dc25a99752d980caeac914f7e48

Array
(
[test] => test
)

Array
(
[lifetime] => 0
[path] => /
[domain] => .example.com
[secure] =>
[httponly] =>
)
This is what you get when I output the session name, session ID, below that the contents of $_SESSION and finally the result of session_get_cookie_params(). As you can see, the session is set.

Now when I run this exact same script on sub2.example.com, I get:

Quote:
PHPSESSID c6425dc25a99752d980caeac914f7e48

Array
(
)

Array
(
[lifetime] => 0
[path] => /
[domain] => .example.com
[secure] =>
[httponly] =>
)
As you can see, same session name/id so you'd think it would work, BUT the $_SESSION array is suddenly empty!

Now when I go back to sub1.example.com after visiting sub2.example.com, I notice that over there the $_SESSION variable is empty too.

Strange. First it's set on sub1, then I visit sub2 and it seems to reset the session instead of using the session date set by sub1.

Any idea what might be causing this?

Reply With Quote


Sponsored Links
  #2  
Old 07-16-2008, 05:34 AM
webcertain webcertain is offline
Web Hosting Guru
 
Join Date: Jun 2008
Location: UK
Posts: 266
hmm this definately looks to be like your session is only valid for one subdomain.

what version of php ?

edit : using apache ? what version ?

from some googling it seems that lots of things can cause this - php config, things that harden security on the server, etc.

what you could do is generate a unique id in php, store this in a database, and use it across both domains, would skip the cookies out entirely

__________________
WebCertain - SEO, PPC, PR - The services you need to increase your site traffic. Specialists in over 35 languages and country-specific SEO! www.webcertain.com


Last edited by webcertain; 07-16-2008 at 05:45 AM.
Reply With Quote
  #3  
Old 07-16-2008, 05:40 AM
ResellerPlanet ResellerPlanet is offline
Aspiring Evangelist
 
Join Date: Aug 2004
Posts: 414
Quote:
Originally Posted by webcertain View Post
hmm this definately looks to be like your session is only valid for one subdomain.

what version of php ?
PHP Version 5.2.6

Here's my session config:

Quote:
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn Off Off
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain .example.com .example.com
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.hash_bits_per_character 4 4
session.hash_function 0 0
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path no value no value
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid 0 0

Reply With Quote
Sponsored Links
  #4  
Old 07-16-2008, 06:19 AM
ResellerPlanet ResellerPlanet is offline
Aspiring Evangelist
 
Join Date: Aug 2004
Posts: 414
Quote:
Originally Posted by webcertain View Post
edit : using apache ? what version ?

from some googling it seems that lots of things can cause this - php config, things that harden security on the server, etc.

what you could do is generate a unique id in php, store this in a database, and use it across both domains, would skip the cookies out entirely
Yes, Apache/2.2.8. I'm running Suhosin... Might that be causing it?

I don't have access to the code on one of my subdomains since it's encoded with Ioncube. Therefore I can't use any other coding tricks such as using a MySQL database.

Reply With Quote
  #5  
Old 07-16-2008, 06:22 AM
webcertain webcertain is offline
Web Hosting Guru
 
Join Date: Jun 2008
Location: UK
Posts: 266
yes, suhosin is definitely a suspect, as it tries to prevent cross site scripting, which is sorta what this is.

edit : suhosin.session.cryptdocroot < this is mentioned on http://bugs.php.net/bug.php?id=43682 , so its worth looking if this value is the cause.

__________________
WebCertain - SEO, PPC, PR - The services you need to increase your site traffic. Specialists in over 35 languages and country-specific SEO! www.webcertain.com

Reply With Quote
  #6  
Old 07-16-2008, 06:33 AM
ResellerPlanet ResellerPlanet is offline
Aspiring Evangelist
 
Join Date: Aug 2004
Posts: 414
Quote:
Originally Posted by webcertain View Post
yes, suhosin is definitely a suspect, as it tries to prevent cross site scripting, which is sorta what this is.

edit : suhosin.session.cryptdocroot < this is mentioned on http://bugs.php.net/bug.php?id=43682 , so its worth looking if this value is the cause.
Yes thanks I just figured it out. The suhosin.session.cryptdocroot (and possibly suhosin.cookie.cryptdocroot) features were causing this. Disabling them resolved my issue.

Thanks!

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Cloud Expo 2013 Web Hosting Events 2013-04-24 17:21:38
Microsoft Reaches Settlement with Nitol Botnet Host 3322.org Web Hosting News 2012-10-05 13:13:16
Web Hosting Sales and Promos Roundup – June 22, 2012 Web Hosting News 2012-06-22 16:36:15
LulzSec Hackers Retaliate After Arrests, Deface Dozens of Panda Security Subdomains Web Hosting News 2012-03-08 10:41:26
HostingCon Day 1 Blog 2011-08-08 22:53:21


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?