2

I have a website www.example.com. Subdomain is sub.example.com. I am using shared server, so I cant access php.ini. In www.example.com login page I am using

session_start();
$_SESSION["UserId"]=$UserId;

So thab I can use $_SESSION["UserId"]. But when I try to access session in sub.example.com it failed. No session available. How I get session variables in subdomain. What are the codes for example.com and also for sub.example.com.

Derick Alangi
  • 1,080
  • 1
  • 11
  • 31
Nidhi
  • 153
  • 2
  • 12
  • there's no codes. you just need to check that your session cookie settings allow for subdomain use. there's any number of other questions on this site for that sort of thing. Once you get the cookies sorted out, the sessions should "Just work". – Marc B Sep 21 '16 at 20:00
  • @MarcB Sir how can i create a cookie in main domain and set UserId in cookie. How i access cookie in subdomain. How to find whether userid exist or not by using php script – Nidhi Sep 21 '16 at 20:05
  • @Nidhi You can read PHP manual or add new question. http://php.net/manual/en/function.setcookie.php – Michas Sep 21 '16 at 20:14
  • http://stackoverflow.com/questions/9153716/sharing-session-variables-between-multiple-subdomains – user1844933 Sep 23 '16 at 03:13

1 Answers1

0

try this put it at the head of the php file, i've used this before

session_name("mySession");
session_set_cookie_params(0, '/', '.example.com');
session_start();
$_SESSION['UserId'] = $UserId; 
Schnecke
  • 502
  • 1
  • 6
  • 18