Search code examples
phpcodeigniter

Codeigniter session not working once controller redirect to another controller


I've been stuck, when I sign up and want to redirect controller to another controller for showing dashboard then that time created session does not working on redirected controller. Here is my sample code.

My Sign up controller :

class Signup extends CI_Controller {

 public function __construct()
 {
    parent::__construct();
    $this->load->helper('url');
    $this->load->model('admin_model');
    $this->load->library('form_validation');
    $this->load->helper('cookie');
    $this->load->library('session');
    $this->load->library('email');
    $this->load->helper('string');
    $this->load->library('upload');
}

public function index() {
   if(!$_POST) {
        $this->load->view('web_pages/signup/signup');
   } else {
            $insert_data = array(
            'firstName' => $signup_data['firstName'],
            'lastName' => $signup_data['lastName'],
            'email' => $signup_data['email'],
            'password' => $signup_data['password'],
            'phoneNo' => $signup_data['phoneNo'],
            'userType' => $signup_data['userType'],
            'image' => $image,
            'createDate' => date('Y-m-d H:i:s')
            );
            $this->db->insert('users', $insert_data);
            $this->session->set_userdata(array(
                    'user_id'       => $insert_data['email'],
                    'userType'      => $insert_data['userType'],
                    'status'        => TRUE
                ));
                
            redirect('dashboard'); //another controller.
        }
   }
}

Below is my dashboard controller

class Dashboard extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->helper('url');
    $this->load->model('admin_model');
    $this->load->library('form_validation');
    $this->load->helper('cookie');
    $this->load->library('session');
    $this->load->library('email');
    $this->load->helper('string');
    $this->load->library('upload');
}

public function index() {
   print_r($_SESSION); die;
}

}

Above dashboard controller doesn't print anything.

Please help me . Thanks in advance.


Solution

  • Print the data in session using

    public function index() {
    $this->load->library('session');
    print_r($this->session->all_userdata());
    }
    

    Check the config file with below params

    $config['sess_expiration']  = 8600;
    $config['sess_match_useragent'] = FALSE;
    

    Also check the cookie config

    $config['cookie_prefix'] = "";
    $config['cookie_domain'] = "";
    $config['cookie_path'] = "/";
    

    Make an entry in autoload.php for session

    $autoload['libraries'] = array('session'); //in autoload.php