2

i have domain and i created a sub domain as well with the name www.join.domainname.com, now the problem is i start session on the main domain login page that is www.domainname.com/support/login.php

all the pages in same domain working properly with session but when i am trying to check the session on : www.join.domainname.com/member.php

i am not getting anything i don't know why?? Plz help me to solve the issue, here is the code of www.join.domainname.com/member.php :

session_start();
$session_key = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : 'empty';
echo $session_key;

it return the result empty.

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
  • Shouldn't it be `$session_key = (isset($_SESSION['userid']) ? $_SESSION['userid'] : 'empty');`? – Viruzzo Nov 23 '11 at 11:41
  • 1
    show us the code that sets the variable in the session. – Jan Dragsbaek Nov 23 '11 at 11:41
  • @JanDragsbaek thanks for your response, the session is working properly on 'www.domainname.com' and its start on the same domain, but i am checking the same session on the 'www.join.domainname.com/' and not getting any thing :( – jogesh_pi Nov 23 '11 at 11:47
  • 1
    See this question for using sessions across subdomains: http://stackoverflow.com/questions/1064243/php-sessions-across-sub-domains – F21 Nov 23 '11 at 11:47
  • Please make sure that you have started session in first line of every page you have used session i.e member.php and login.php. session should be started at the top of the page. no even any white space before. Please show how you have created session. Regards – LIGHT Nov 23 '11 at 11:42
  • @mgraph, yes i also thinking that but i am beginner in php, that's why i am not sure,, – jogesh_pi Nov 23 '11 at 11:48

2 Answers2

2

You have to set the session cookie domain to .domainname.com so that it can be accessible to all of its subdomain.

you can use the session_set_cookie_params to do this.

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

Alternatively, you can set the session cookie domain with ini_set

ini_set('session.cookie_domain','.domainname.com');
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
0

From my previous experience to make your session usable across domain/sub domain you need to use the session.cookie_domain setting e.g

// Start the session
DEFINE('COOKIE_BASE_DOMAIN_NAME', '.domain.com');
$some_name = session_name("domain-name"); 
ini_set('session.cookie_domain', COOKIE_BASE_DOMAIN_NAME);
session_start();
PHPology
  • 1,017
  • 6
  • 12