CodeIgniter : Loosing session data after login

Posted on August 10, 2013 by Suresh Kamrushi in PHP

In Codeigniter after login I am storing all the data into session and redirecting to some controller.But i am loosing all values in that controller and code redirect back to login page. I try every trick to debug this issue but fail!!!

I don’t know from where i got idea to reduce some values from session, Magic.. it start working fine..

Now my question is– why this is happening? Is there any upper limit of values to be store in session in codeIgniter?

Explanation:
By default CodeIgniter stores the session data in a cookie, which has an upper limit of 2KB-4KB in size depending on browser.

If you are trying to store more than 4KB of data in the session you will start running into issues.

Solution:
The easiest solution is to store the session details in the DB.The CodeIgniter session documentation details the process for storing the session in the DB.it’s a matter of setting up a table, and changing a couple of config parameters. Below are the changes you need to do-

Update below variable in Config file (application/config/config.php):

$config['sess_use_database'] = TRUE;
$config['sess_table_name']   = 'ci_sessions';

Created new table for storing session-

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(45) DEFAULT '0' NOT NULL,
user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id),
KEY `last_activity_idx` (`last_activity`)
);
  Comments or questions are welcome