Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Session Data not available in other pages after login

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.

like image 970
jswat Avatar asked Mar 15 '10 17:03

jswat


People also ask

How to store session data in database CodeIgniter?

Once you have created your database table you can enable the database option in your config. php file as follows: $config['sess_use_database'] = TRUE; Once enabled, the Session class will store session data in the DB.

How to pass session in CodeIgniter?

Add Session Data The same thing can be done in CodeIgniter as shown below. $this->session->set_userdata('some_name', 'some_value'); set_userdata() function takes two arguments. The first argument, some_name, is the name of the session variable, under which, some_value will be stored.

How to set session variable in PHP CodeIgniter?

php create an array to store your session data. $new_data = array( 'username' => 'martin', 'email' => '[email protected]', 'user_logged => TRUE ); $this->session->set_userdata($new_data); Then this is how to call your session data(create a variable and assign it the value of one of the session data you need):

How to set and unset session in CodeIgniter?

print_r( $_SESSION [ 'logged_in' ]); ?> Unset the session variable: The session variable can be unset by assigning it to a NULL value. This destroys the value stored at this key value.


3 Answers

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.

like image 177
John McCollum Avatar answered Oct 06 '22 01:10

John McCollum


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.)

like image 40
Rei Avatar answered Oct 05 '22 23:10

Rei


If anybody encounters this problem, go to config/config.php and make sure $config['sess_use_database'] is set to TRUE. This will save all session data to your database -- which worked for me.

If you don't have a table named 'sessions', run this SQL command:

CREATE TABLE  `sessions` (
`session_id` VARCHAR( 40 ) NOT NULL DEFAULT  '0',
 `ip_address` VARCHAR( 16 ) NOT NULL DEFAULT  '0',
 `user_agent` VARCHAR( 120 ) DEFAULT NULL ,
 `last_activity` INT( 10 ) UNSIGNED NOT NULL DEFAULT  '0',
 `user_data` TEXT NOT NULL ,
PRIMARY KEY (  `session_id` ) ,
KEY  `last_activity_idx` (  `last_activity` )
) ENGINE = MYISAM DEFAULT CHARSET = utf8;
like image 45
hohner Avatar answered Oct 05 '22 23:10

hohner