views:

395

answers:

2

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  A: 

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.

John McCollum
Thanks, that seems to have fixed the issue! I prefer using native PHP sessions anyways.
jswat
Glad it helped. I'm not overly enamoured with Codeigniter's session handling!
John McCollum
A: 

Hi, Im also new to code igniter. Juz to give an idea. Have u put session_start() in your controller? here is the example:

class Cuser extends Controller {

function Cuser()
{
    parent::Controller();
    session_start();
    $this->load->helper('url');     
}

}

maybe im wrong...but u can try it...:)

warhead2020
Thanks, but Codeigniter doesn't use traditional PHP session. It actually stores it's 'session' data in cookies (or in a database if you have it set up). Therefore, there is no need to add the session_start() code. The solution John McCollum gave fixed my issue.Thanks though!
jswat