How to Use Session in CodeIgniter

How to Use Session in CodeIgniter

The session class allow you a users state and track their activity while browsing site. The session class stores session information for each user as serialized data in a cookie also store the session data in a database for added security.

A session simply an array contains information like
  • User’s unique session id
  • User’s  IP address
  • User’s user agent data
  • Last activity time stamp

All data store in a cookie as a serialized array like

                'session_id      => random hash,
                'ip_address'    =>'user IP address',
                'user_agent'   => 'user agent data',
                'last_activity'  => 'timestamp'

Session Work?
When a page in loaded, session will check to use if valid session data exists in the user session cookie. If does not exit or expired a new session will created and saved in cookie. If session exit, its information will updated also cookie will be updated with each update session id will be regenerated.

In session need toconfigure like

Encryption Key
Before we can use session in codeigniter, we need to configure encryption key
Go to root folder where codeIgniter in installed like application\config\config.php

        $config['encryption_key'] = '';
To configure
        $config['encryption_key'] = '123456';

Initialize Session
  • Manually initialize the session

  • Auto-loaded by the system

       Go to root folder where codeIgniter in installed path like application\config\autoload.php
       Add session in array
       $autoload['libraries'] = array('session');

      Once loaded the session library object will be available using

How to Use Session

  • Get/Retrieve Session Data


       This Function return null, if the item you are trying to access does not exit.
  • Set/Add Session Data

       Add userdata single value at a time
       $this->session->set_userdata('name', 'value');
       Add multiple userdata by passing an associative array containing  your new data
       $newdata = array(
                                 'username'  => 'john',
                                 'email'        => '',
                                 'logged_in'  => true
  • Verify userdata value exit

       If you want to verify that a userdata value exit, call has_userdata() function 
       $name= $this->session->has_userdata('name');
       This function return Boolean true, if a userdata exit otherwise false
  • Removing Session Data

       To remove a userdata

       To remove multiple userdata at once use associative array
       $array_items = array(
                                     'username'=>' ',
                                     'email’=>' '

  • Destroying a Session

       To clear the current session

       This function should be last one called all userdata variables will no longer be available. if you only want some items destroyed and not all, use unset_userdata()

                       I hope, it helped you to understand, How to use Session in CodeIgniter......Thanks


Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment


Post a Comment

Thanks For Visiting Blog...