CodeIgniter Session Management

When building the websites, we often need to track user’s activity and some code condition management and for this purpose, we have to use the session. CodeIgniter has provided session class for this purpose. CodeIgniter Session Driver

CodeIgniter comes with a few session storage drivers:

  1. files (default; file-system based)
  2. database
  3. redis
  4. memcached

Initializing a Session library

Sessions data are available globally through the site but to use those data we where we working need to initialize the session. We can do that by executing the following line in constructor. Or in application/config/autoload.php file 

To autoload resources, open the application/config/autoload.php file and add the item you want loaded to the autoload array. You’ll find instructions in that file corresponding to each type of item.


We can do that by executing the following line in constructor function inside.


After loading the session library, you can simply use the session object as shown below.


If you’ve used sessions in PHP before, you should be familiar with PHP’s $_SESSION super global (if not, please read the content on that link).

CodeIgniter gives access to its session data through the same means, as it uses the session handlers’ mechanism provided by PHP. Using session data is as simple as manipulating (read, set and unset values) the $_SESSION array.

Adding Session Data

$_SESSION['item'] = array('key'=>'data');

$this->session->set_flashdata('item', 'value');
$this->session->set_tempdata('item', 'value', 300);

Destroying a Session CodeIgniter 


CodeIgniter Session Preferences

CodeIgniter will usually make everything work out of the box. However, Sessions data  are a very sensitive component of any application, so some careful configuration must be done. Please take your time to consider all of the options and their effects.

If you want to change session driver and storage path or any more session settings. you'll find the following Session related preferences in your application/config/config.php file:

Default session setting

$config['sess_driver'] 			= 'files';
$config['sess_cookie_name'] 	= 'ci_session';
$config['sess_expiration'] 		= 7200;
$config['sess_save_path'] 		= null;
$config['sess_match_ip'] 		= FALSE;
$config['sess_time_to_update'] 	= 300;
$config['sess_regenerate_destroy'] = FALSE;

Database Driver

The ‘database’ driver uses a relational database such as MySQL or PostgreSQL to store sessions

if you would like to use ‘tbl_sessions’ as your table name, you would do this:

$config['sess_driver'] 	  = 'database';
$config['sess_save_path'] = 'tbl_sessions';

Database Table For MySQL :

        `id` varchar(128) NOT NULL,
        `ip_address` varchar(45) NOT NULL,
        `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
        `data` blob NOT NULL,
        KEY `ci_sessions_timestamp` (`timestamp`)

Database Table For PostgreSQL:

CREATE TABLE "tbl_sessions" (
        "id" varchar(128) NOT NULL,
        "ip_address" varchar(45) NOT NULL,
        "timestamp" bigint DEFAULT 0 NOT NULL,
        "data" text DEFAULT '' NOT NULL

CREATE INDEX "ci_sessions_timestamp" ON "tbl_sessions" ("timestamp");

You will also need to add a PRIMARY KEY depending on your ‘sess_match_ip’ setting. The examples below work both on MySQL and PostgreSQL:

// When sess_match_ip = TRUE
ALTER TABLE ci_sessions ADD PRIMARY KEY (id, ip_address);

// When sess_match_ip = FALSE

// To drop a previously created primary key (use when changing the setting)

Stack Overlode is optimized Tutorials and examples for learning and training. Examples might be simplified to improve reading and learning and understand. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using Stack Overlode, you agree to have read and accepted our terms of use, cookie and privacy policy.