Solved

Problem with session data across subdomains and protocols

Posted on 2004-09-22
3
3,240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Expert Comment

by:peyox
ID: 12129233
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 12131518
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
ID: 12132805
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 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