CodeIgniter Session

Previous Page

Session storage drivers in CodeIgniter:

  • files 
  • database
  • Redis
  • memcached

Initialization of a session:

Sessions run globally every time the page loads, the session class is initialized in the controller or can also be autoloaded by our system.the session class is run in the background all the time by default, we just have to create a class to make our system read it, we can also manipulate the session if needed.

Adding a session class manually inside the controller:


Calling a session:


Working of session:

  • Every time the page loads, if the cookies sent by the user are valid or not.
  • If the cookies are invalid or expired, a new session will be generated and saved.
  • If cookies are matched and the session is valid, it starts updating all the information with each activity of a user, all this is done with the help of session ID.
  • Once a session is initialized it will keep running automatically.
  • All the work like reading, updating, and writing s sessions is done automatically.

Session Data:

  • Session data is stored in an array that is associated with its distinct session ID and cookies.
  • Unlike PHP sessions, Codeigniter also has similar working like reading, inserting, and updating data in arrays but it is more simplified.

Retrieving data from session:

$_SESSION is used to get the bit of code from the session array, globally.



Otherway using getter:


Using userdata() method:


Using item name:

$name = $_SESSION['item-name'];
// else:
$name = $this->session->'item-name'
// else:
$name = $this->session->userdata('item-name');

We can also retrieve user data using $userdata():

// else,

Adding data to the session:

  • When a client logs into our web application, we get the username, email, and other details we have requested in our query.
  • All of this data from the user is assigned to the $_SESSION which is an array.
  • Or we can use another method called set_userdata().


$newdata = array(

        'user_name'  => 'peter',
        'email_id'     => '',
        'is_logged_in' => TRUE



We can also add one value at one point.

 $this->session->set_userdata('user_name', 'value'); 

We can check for a value in session using:


Other way to call the data from the session using has_userdata() 



Removing data from a session:

unset () method id used to remove any data from the session.



// else  multiple values:





Unlike setuserdata() method we can use unset_userdata() method also:


Unset multiple values:

$array_items = array('user_name', 'email_id');


Destroying a Session:

While the user logs out or clear history on their browser the session gets destroyed with the help of session_destroy() function or sess_destroy() function both of these functions work similarly.





  • Flashdata is a form of session data.
  • This data is stored for the next server requests.
  • After the next request, the data gets cleared.
  • Flash data is usually used to display error/warning and one-time message/information to the user.


Marking existing session data item as “flashdata”:


Marking multiple items as a flashdata:

$this->session->mark_as_flash(array('item_1', 'item_2'));

Adding FlashData:

$_SESSION['item'] = 'value';


Alternate function to add flashdata using flash setdata() function:

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

Reading items from flashdata:






Reading flashdata using keep_flashdata() function:


$this->session->keep_flashdata(array('item1', 'item2', 'item3'));


  • Temp data is a form of session data with a specified expiration time with it.
  • The value is removed automatically if the specified time is expired or the session expires.
  • To mark the session data as a tempdata we use mark_as_temp().



$this->session->mark_as_temp('item', 500);

Marking multiple items a temp data in session:


        'item'  => 500,

        'item_2' => 1000


Adding Tempdata:

$_SESSION['item'] = 'value';

$this->session->mark_as_temp('item', 600); // Expire in 10 minutes

Using alternative method set_tempdata():

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

Using alternative method set_tempdata() with array:

$tempdata = array('new_user' => TRUE, 'message' => 'Hello from the session!');

$this->session->set_tempdata($tempdata, NULL, $expire);

Reading Tempdata:


Using tempdata() method:


Retrieving all data from tempdata using tempdata():


Removing tempdata before it expires:


Removing tempdata using unset_tempdata() function before expiration:



Previous Page

Treding Tutorial

Popular Tutorials

to Learn now