Codeigniter Session Data not available in other pages after login


Question

So, I have set up a login page that verifies the user's credentials, and then sets codeigniter session data 'email' and 'is_logged_in' and a few other items. The first page after the login, the data is accessible. After that page, I can no longer access the session data. In fact, if I try reloading that first page, the session data is gone.

I have tried storing it in the database, storing it unencrypted (bad idea I know, but it was for troubleshooting), and storing it encrypted. I have autoloaded the session library in config.php.

Here's an example of the code I'm using to set the session data:

$data = array(
                    'email' => $this->input->post('username'),
                    'is_logged_in' => true 
                );
                $this->session->set_userdata($data);

And to retrieve it, I'm using :

$this->session->userdata('email');

Or

$this->session->userdata('is_logged_in');

I've done lots of work with PHP and cookies, and sessions before, but this is my first project with Codeigniter and I'm perplexed.

Could it have something to do with directory issues? I have the login page and process controlled by a 'login' controller, and then it redirects to a 'site' controller.

Thanks for your help, and please let me know if I need to clarify anything.

1
9
3/15/2010 5:28:55 PM

Accepted Answer

Your code looks fine. I've had numerous issues with Codeigniter's session library, including something similar to what you mentioned.

Consider looking at the Native Session library to resolve your issue.

6
3/27/2013 2:42:13 PM

Just so anyone else trying to use CI's sessions has something to try:

I have been looking around for a solution to the same problem, and after many dead ends, I re-investigated my config.php.

Therein, I noticed that $config['cookie_domain'] wasn’t set properly. Upon setting this (and setting my cookie_prefix to ''), sessions now work correctly.

Since the cookie needs to be set to your domain in order to be pulled correctly, it was simply not finding a cookie reference for my session, and making a new one every time.

(Setting up database sessions helped immensely with figuring this out.)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon