Solved

php session does not carry across subdomains

Posted on 2009-05-07
6
402 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
6 Comments
 
LVL 50

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
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…
The viewer will learn how to dynamically set the form action using jQuery.

777 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