Solved

php session does not carry across subdomains

Posted on 2009-05-07
6
406 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 52

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
Technology Partners: 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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 you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
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 look for a specific file type in a local or remote server directory using PHP.
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 …

710 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