Solved

Problem with session data across subdomains and protocols

Posted on 2004-09-22
3
3,222 Views
Last Modified: 2012-06-27
Ready for this one, everyone?

I'm having a problem accessing a PHP session across subdomains.

I have a Linux/Apache server running PHP 4.3.2 with one domain on it. Let's call it foo.com. The domain has the usual 'www' subdomain plus another subdomain, 'secure'. I have set up the httpd.conf file so that the documentRoot for secure.foo.com is /home/foo/secure and the documentRoot for www.foo.com is /home/foo/www. The two subdomains also have unique IP addresses.

Here's the applicable section of the httpd.conf file (the IP addresses and paths have been changed to protect the ignorant):

NameVirtualHost 1.2.3.4:80
<VirtualHost 1.2.3.4:80>
DocumentRoot /home/foo/www
ServerName www.foo.com
ServerAlias foo.com
</VirtualHost>
<VirtualHost 5.6.7.8:443>
DocumentRoot /home/foo/secure
ServerName secure.foo.com
SSLEngine On
SSLCertificateFile /path/to/foo.com.crt
SSLCertificateKeyFile /path/to/foo.com.key
</VirtualHost>

The DNS for the domain (foo.com) is set up as follows:

<SOA and other stuff...>
      IN A 1.2.3.4
mail  IN A 2.3.4.5 ;mail on another server
www   IN A 1.2.3.4
secure IN A 5.6.7.8

With this setup, www.foo.com only accepts http traffic and secure.foo.com only accepts SSL (https) traffic.

If I start a PHP session on http://www.foo.com/page.php I can access all of the values I toss into the session from any page on the site. However, as soon as I pull up a page on https://secure.foo.com (and call session_start), the session variables are not there (print_r ($_SESSION) outputs 'array ()').

I have session.cookie_domain in php.ini set to 'foo.com' (I also tried '.foo.com').

It's my understanding that cookies -- including session cookies -- are supposed to be available across subdomains. Does the fact that the subdomains are using different transfer protocols (http v. https) make a difference?

I've looked everywhere for an answer to my problem. Please put me out of my misery and tell me it's something stupid I've overlooked!!!
0
Comment
Question by:dbinteractive
3 Comments
 
LVL 6

Expert Comment

by:peyox
Comment Utility
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
Comment Utility
When you are setting cookies in order for them to be available across all subdomains the 5th parameter (domain) must be prefixed with a dot. eg. setcookie("name","value",time()+3600,"/",".foo.com");

The same applies to the session cookie, in your php.ini file you will find a line that reads something like:

; The domain for which the cookie is valid.
session.cookie_domain =

Add your domain prefixed with a . and don't forget to restart your webserver afterwards. eg:

; The domain for which the cookie is valid.
session.cookie_domain = .foo.com
0
 

Author Comment

by:dbinteractive
Comment Utility
I should have known -- It was something stupid I overlooked.

I didn't realize I had to restart Apache after making changes to php.ini.

For future EE users who may encounter the same problem: in order for cookies (session or otherwise) to be available across subdomains, you must do as Diablo84 (and the PHP manual) says: set the cookie_domain to .foo.com. You can do this either by changing the session.cookie_domain value in your php.ini file or by calling ini_set ("session.cookie_domain", ".foo.com") before you call session_start ().

Funny enough, I had tried both methods yesterday. Neither worked. The php.ini change obviously didn't work because I neglected to restart Apache. Why the second method (ini_set ...) didn't work is beyond me. All I know is I came in this morning and the session data were magically available across my subdomains.

Could be that Apache restarted overnight. Could be that the session ID I was using yesterday kept getting recycled and GC cleaned it up overnight. Either way, it's working now.

Although the problem "fixed itself", I'll give the points to Diablo84 since he reminded me that I have to restart Apache.

Thanks, experts!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now