Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php session does not carry across subdomains

Posted on 2009-05-07
6
Medium Priority
?
417 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 53

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 2000 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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
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.

722 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