3
votes

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.

2
Does any of your data get inserted into the database? I think $signup_data wont have any data in it. use $this->input->post('email') - theEUG
yes @theEUG you are right $signup_data is not define so it will goes blank then session value also goes to blank that of the reason session is not working - ImBhavin95
i tried with data and i able to printed session value on first controller but i didn't get on next controller. - saddam

2 Answers

2
votes

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
0
votes

See this updated code segment

    if(!$_POST) {
        $this->load->view('web_pages/signup/signup');
   } else {
            $insert_data = array(
            'firstName' => $this->input->post('firstName'),
            'lastName' => $this->input->post('lastName'),
            'email' => $this->input->post('email'),
            'password' => $this->input->post('password'),
            'phoneNo' => $this->input->post('phoneNo'),
            'userType' => $this->input->post('userType'),
            'image' => $image,
            'createDate' => date('Y-m-d H:i:s')
            );
            $this->db->insert('users', $insert_data);
            $this->session->set_userdata(array(
                    'user_id'       => $this->input->post('email'),
                    'userType'      => $this->input->post('userType'),
                    'status'        => TRUE
                ));

            redirect('dashboard'); //another controller.
        }
   }

Then simply to access the session data

echo $this->session->userdata('firstname');