0

Searched through a few solutions, none of which seem to work for me. Most solutions like this one: HERE seem to point to using:

session_set_cookie_params(0, '/', '.domain.com');
session_start();

I am either not understanding the post or implementing incorrectly. I have the domains below and log in through domain.com

domain.com
sub.domain.com 

I've implemented the code above at the top of my login.php life on domain.com and upon login I set $_SESSION[id] = xyz;

I have session_start(); at the top of my index.php page on sub.domain.com

when I var_dump($_SESSION); on domain.com i get a value returned. when I try it on sub.domain.com I get array(0) { }

I've also tried putting the code below at the very top of sub.domain.com but still get nothing.

session_set_cookie_params(0, '/', '.domain.com');
session_start();

So clearly i'm doing something wrong, but I have no idea what. Thanks.

Community
  • 1
  • 1
Guage
  • 85
  • 10

2 Answers2

1

1、One method is configuring your php.ini file,do like this:

session.cookie_domain = .domain.com

2、Second is using ini_set();

ini_set('session.cookie_domain', '.domain.com');

3、Third is using session_set_cookie_params(),your error is here.More detail about it,look:http://php.net/manual/en/function.session-set-cookie-params.php

The effect of this function only lasts for the duration of the script. Thus, you need to call session_set_cookie_params() for every request and before session_start() is called. 

so,you need call the method for every request.

stary
  • 36
  • 4
  • I don't have access to my php.ini file, so i've tried option 2 I did a var_dump($_COOKIE); on domain.com and on sub.domain.com and I get 2 separate PHPSESSID. on domain.com I get: ["PHPSESSID"]=> string(32) "326a307069e831007f6e38cfbf4ea826" on sub.domain.com I get: ["PHPSESSID"]=> string(32) "bbdacebb93944e7794da593b208e7a54" – Guage Feb 26 '14 at 18:12
0

I want to explain what solved this for me. The use of "session.cookie_domain" is the only method that worked for me.

I have 3 files:

login.php     - in domain.com  
user_home.php - in domain.com

sub_index.php - in sub.domain.com

I added the code below to the top of each file without any resolve:

ini_set('session.cookie_domain', '.domain.com');
session_start();

Reason is because login.php is accessed through index.php on domain.com and I did not add the code to index.php - login is at /index.php?script=login

Once I included the code at the top of all the relevant files the var_dump($_COOKIE) showed the same value for PHPSESSID across domain.com and sub.domain.com

Hope it can help someone else.

Guage
  • 85
  • 10