cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Go to solution
Highlighted

How to share Session between domain and subdomain.

I want to know how to use a session of the domain into subdomain after login and if first I login to a subdomain then it also login to main domain.

I'm using php oops and i created session class. This is my session class. How can i use session of domain into subdomain and session of subdomain into main domain.

 

<?php

class Session {
    
    private $user_id;
    public $email;
    public $mobile_number;
    public $username;
    
  
    public const MAX_LOGIN_AGE = 60*60; // 1 day
    public function __construct() {
         session_start(); // turn on sessions if needed

      
        $this->user_stored_login();
    }


    public function log_user($user) {
        if($user){
            // prevent session fixation attacks
            session_regenerate_id();
           $this->user_id = $_SESSION['user_id'] = $user->id;
         $this->email = $_SESSION['email'] = $user->email;
         $this->mobile_number = $_SESSION['mobile_number'] = $user->mobile_number;
            $this->last_user = $_SESSION['last_user'] = time();
        }
        
        return true;
    }

    


    public function is_user_in(){
        
        //return isset($this->admin_id);
         return isset($this->user_id) && $this->last_user_is_recent();
     }
    


    public function user_logout() {
        unset($_SESSION['user_id']);
          unset($_SESSION['email']);
          unset($_SESSION['mobile_number']);
          unset($_SESSION['last_user']);
        unset($this->user_id);
           unset($this->email);
           unset($this->mobile_number);
           unset($this->last_user);
        return true;
    }


    

    private function user_stored_login() {
     if(isset($_SESSION['user_id'])) {
            $this->user_id = $_SESSION['user_id'];
            $this->email = $_SESSION['email'];
            $this->mobile_number = $_SESSION['mobile_number'];
            $this->last_user = $_SESSION['last_user'];
        }
    }
    



    private function last_user_is_recent() {
        if(!isset($this->last_user)) {
            return false;
        }
        elseif(($this->last_user + self::MAX_LOGIN_AGE) < time()) {
            return false;
        }
        else {
            return true;
        }
    }
    
    public function message($msg="") {
        if(!empty($msg)) {
            // THen this is a set message
            $_SESSION['message'] = $msg;
            return true;
        }
        else {
            // Then this is a get message
            return $_SESSION['message'] ?? '';
        }
    }
    
    public function clear_message() {
           unset($_SESSION['message']);
    }
}





?>
1 REPLY 1
Helper VI Helper VI
Helper VI
Solution

Re: How to share Session between domain and subdomain.

@Satyajeet17 

Hi there,

 

I've never done this before but I have a few ideas.

1) Save the session data in a database that both main and sub domain have access to.  Pull and store data on each request.  You'll have to store a unique identifier via cookies.

 

2) Post data to the other domain.  Then capture it like $_SESSION['data'] = json_decode($_POST['data']);

 

I recommend the 1st one though that adds 2 extra calls to your DB.

~Jan Mykhail Hasselbring Web Administrator @ fullstackwebsolution.com