codeigniter Using Sessions Handling Session Data


A session is simply an array consisting of the following user information:

  1. The user's unique Session ID (this is a statistically random string with very strong entropy, hashed with MD5 for portability, and regenerated (by default) every five minutes)
  2. The user's IP Address
  3. The user's User Agent data (the first 120 characters of the browser data string)
  4. The "last activity" time stamp.

Source (what-is-session-data)

To retrieve session data

such as the SessionID:


Note - for Codeigniter 3.x, you can use the above syntax, but the concept or magic getters has been introduced, where you can use $this->session->session_id.

Remember that the userdata() returns NULL if the session item doesn't exist.

To retrieve all session data


To Set Session Data

the set_userdata() method allows you to set data into your session, the following example demonstrates an example array you wish to insert:

$newdata = array(
        'username'  => 'johndoe',
        'email'     => '',
        'logged_in' => TRUE


You can also set one data at a time, for example:

$this->session->set_userdata('some_name', 'some_value');


$some_name = 'some_value';

To Remove Session and Session Data


This method also accepts an array of item keys to unset:

For Codeiginter 3.x:

$array_items = array('username', 'email');


For Codeiginter 2.x (this legacy syntax doesn't support 3.x):

$array_items = array('key' => 'value');