0

I have a login in subdomain. And when users logged I want to redirect them to main domain dashboard. I am not able to maintain the session from subdomain to main domain. I have tried almost all methods like below. None of them works

setcookie ("PassMySessionName", $_SESSION['user_name'], time() - 3600, "/", "example.com");

Also this:

php_value session.cookie_domain ".domain.com"

But setcookie method allow me to pass session from main domain to sub domain.

    ini_set('session.cookie_domain', '.example.com');
    $currentCookieParams = session_get_cookie_params();

    $rootDomain = '.example.com';

    session_set_cookie_params( 
     $currentCookieParams["lifetime"], 
     $currentCookieParams["path"], 
      $rootDomain, 
      $currentCookieParams["secure"], 
      $currentCookieParams["httponly"] 
     ); 

    if(!empty($_SESSION)){

    $cookieName = session_id();

    setcookie('PHPSESSID', $cookieName, time() + 3600, '/', $rootDomain); 

      }

      if(isset($_COOKIE['PHPSESSID'])){

      session_name($_COOKIE['PHPSESSID']); 
      }

Please let me know any other way to pass the session from subdomain to main domain.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Some more code would had been helpful. But if at subdomain $_SESSION['user_name'] = $username; and at main domain $_SESSION['username'] = $username; then the session will not be passed – hans-könig Dec 23 '17 at 17:25
  • I have used the below code which works well when session set in main domain pass to subdomain. – hate_w_p_h Dec 23 '17 at 17:27
  • Then that is something. May be you can check how you create the session at your main domain. It could be that the session comes in from subdomain but is refused due to some 'if' statement defination – hans-könig Dec 23 '17 at 17:33
  • @haslutter session created in subdomain. But that is not passed to domain.com... And i have updated my question with my code try – hate_w_p_h Dec 23 '17 at 17:37
  • This can also be helpful https://stackoverflow.com/questions/13762527/php-session-variable-from-subdomain-to-main-domain – hans-könig Dec 23 '17 at 18:47
  • I've just tested this if(isset($user) && isset($password)){if(($user == 'stackoverflow') && ($password == 'stackoverflow')){$_SESSION['testing'] = $user;header('Location: http:// example.com/index.php');exit();}else{header('Location: login.php');exit();}} from my sub domain to main domain and it works. – hans-könig Dec 23 '17 at 19:15
  • thanks @Hanslutter it works as expected...so grateful to you and so happy too.. You have saved my hours...Happy Christmas :) – hate_w_p_h Dec 24 '17 at 09:01
  • Fantastic! Merry Christmas too – hans-könig Dec 24 '17 at 15:03

1 Answers1

0

(Posted solution on behalf of the question author)

I have added the below session value in the main domain where we assign the session value.

      setcookie ("PassMySessionName", $_SESSION['user_name'], time() - 3600, "/", "example.com");

And it works. When I login from my subdmain, and it is redirected to my main domain's user dashboard.

halfer
  • 19,824
  • 17
  • 99
  • 186