Solved

php session does not carry across subdomains

Posted on 2009-05-07
6
404 Views
Last Modified: 2013-11-10
I have an issue where I need a script to read the same session no matter what subdomain the person is using. Anotherwords, I need the person to stay logged in even if they jump subdomains. I tried setting the below at the top of every page but it still doesn't work. When I try to read the session on a new subdomain it just creates a brand new one.
ini_set("session.cookie_domain", ".domain.com");

Open in new window

0
Comment
Question by:MeridianManagement
[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
6 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 24332352
subdomains and domains don't share sessions.
0
 
LVL 4

Expert Comment

by:aconrad
ID: 24332366
0
 
LVL 7

Accepted Solution

by:
ycTIN earned 500 total points
ID: 24332388
1. ini_set need before the session.start()
2. should have same session name

ini_set("session.cookie_domain", ".domain.com");
session_name("myapp");
session_start();

http://www.php.net/manual/en/function.session-set-cookie-params.php

if still not work, you can try storage the session id into cookie with domain settings
http://www.php.net/manual/en/function.setcookie.php

session_id(<read from cookie>);
session_start();

0
Independent Software Vendors: 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!

 
LVL 2

Author Closing Comment

by:MeridianManagement
ID: 31579279
I'm not sure exactly which one worked, but I also added php_value session.cookie_domain ".domain.com" to an .htaccess file and now it is working perfectly! I'm not going to break it to find out exactly which fix worked though, so I'll assume they all worked in concert =)
0
 
LVL 4

Expert Comment

by:aconrad
ID: 24332399
aw well...
<?php 
 
// ====  script to go on your http://domain.com/
 
session_set_cookie_params(time()+3600,null,'.tzaka.ro');
session_start();
 
$_SESSION['testing'] = 'one';
 
?>
$_SESSION['testing'] = 'one';<br>
<A href="http://sub.domain.com/script.php">visit subdomain</a>
 
// ====  script to go on your http://sub.domain.com/
<?
session_id($_COOKIE['PHPSESSID']);
session_start();
 
echo 'got $_SESSION[\'testing\'] ='.$_SESSION['testing'];
?>

Open in new window

0
 
LVL 2

Author Comment

by:MeridianManagement
ID: 24332410
thanks aconrad, I'm sorry I accepted answer a little too quickly
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

735 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