0

I need to share a session between two subdomains.

I have these domains:

http://example.com
https://secure.example.com

And I tried session sharing with this way:

<?php ini_set("session.cookie_domain", ".example.com"); session_start(); ?>

And this also

<?php session_set_cookie_params ( 0,"/" ,".example.com"); session_start(); ?>

But both seems not working!

How can I make it works?

Sorry for bad English

Alfred Woo
  • 648
  • 1
  • 6
  • 23
  • Check here: http://stackoverflow.com/questions/1064243/php-sessions-across-sub-domains/1457582#1457582 – jeroen Jan 07 '14 at 02:04
  • What domain is the session id cookie finally set to? – zerkms Jan 07 '14 at 02:04
  • @jeroen: I don't see anything additional to what OP already has in your answer – zerkms Jan 07 '14 at 02:05
  • @zerkms Starting with `$some_name = session_name("some_name");` worked for me and some other people. – jeroen Jan 07 '14 at 02:06
  • @jeroen: it's not possible. The name just should be the same. If you don't change it - it would be the same default value. – zerkms Jan 07 '14 at 02:07
  • @zerkms It really did :-) And as you can see I am not the only one. But it was a long time ago on a server with php 5.2. – jeroen Jan 07 '14 at 02:08
  • Try setting a **session name** before it: `$some_name = session_name("some_name"); session_set_cookie_params ( 0,"/" ,".example.com"); session_start();` – Cilan Jan 07 '14 at 02:08
  • 1
    @jeroen: that's reeeeeeeeeeeally weird ;-) It shouldn't affect it, but seems like it might (according your and other people experience). that's how php works )) – zerkms Jan 07 '14 at 02:09
  • 1
    @zerkms If you find some obscure, undocumented thing that works and has no negative side-effects, just move on :-) – jeroen Jan 07 '14 at 02:12
  • session_name not works either – Alfred Woo Jan 07 '14 at 02:21

2 Answers2

0

I don't think sessions are shared across sub-domains. Instead assign the value of the session to a cookie. The cookies are shared.

setcookie("TestCookie", $value, time()+3600);

get the value of the cookie by using this:

$_COOKIE['TestCookie'];
Eegan
  • 1
0

Solved.

I created .htaccess file with this content:

php_value session.cookie_domain ".example.com"

Alfred Woo
  • 648
  • 1
  • 6
  • 23