Naveen Chauhan
Naveen Chauhan

Reputation: 2026

CodeIgniter Session - i am not getting my userdata in other controller

here is my code to set session

$login_data = array('logged_in'=>TRUE, 'user_id'=>$userid);
$this->session->set_userdata($login_data);
redirect('dashboard');

above code creates a new entry in ci_session table in my db and it has logged_in value

In dashboard controller i am checking session. Below is my code in dashboard controller

function __construct(){
        parent::__construct();
        $logged_in = $this->session->userdata('logged_in');
        if(!$logged_in){
            redirect("welcome");
        }
    }

In above constructor block i am not getting the value of logged_in.

I have also tried to print all my userdata using var_dump($this->session->all_userdata) but all I get is an empty string.

Please tell me what could be the reason behing it. I also searched for cookie but i didn't find that in my computer. below is my config detail

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = TRUE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

Upvotes: 0

Views: 7824

Answers (3)

Naveen Chauhan
Naveen Chauhan

Reputation: 2026

I have solved my problem. I just forgot to change my Cookie Related Variables in config. –

Upvotes: 2

Jordan Arsenault
Jordan Arsenault

Reputation: 7418

Is the library being autoloaded? Check config/autoload.php:

$autoload['libraries'] = array('database', 'session', 'form_validation');

Or call $this->load->library('session'); right before using the class.

Upvotes: 0

Tom Howell
Tom Howell

Reputation: 64

My first guess is being you're doing this in the __construct method which is executed when the class is initialized - before the page is even loaded and your sessions data is set.

$logged_in = $this->session->userdata('logged_in');
    if(!$logged_in){
        redirect("welcome");
    }

Will work if you add in into the beginning of the page method.

Upvotes: 0

Related Questions