Sessions are one of the most important part for any web applications, as it helps to maintain user’s “state” and to track their activity when they visit your website.

Here we have use CodeIgniter’s session class that will maintain and help to store session information for each user.

To use this class you need to first initialize or load the session library of CodeIgniter in your controller constructors or you can also go for $autoload in config file, so that it can be auto-loaded by the system.

To load session library manually use below code –



Note: The Session class in CodeIgniter does not use native PHP sessions, as It generates its own session data.

Following are some syntax that are mainly used for session data:-

To retrieve session data:

$this->session->userdata('item'); // Where item is the array index like session id

To add custom session data:

$this->session->set_userdata($array); // $array is an associative array of your new data

To retrieve all session data:

$this->session->all_userdata() // Read all session values

To remove all session data:

$this->session->unset_userdata('some_name'); // Removing values from session

We have made an example which will let you know how we have used the above mentioned syntax and how they works.

To run this example locally at your end you can create the files and copy the code. If you wish to download the files, you can click using the download link given below.


 Watch the live demo or download code from the link given below

How to run file:



Tutorial Scripts in detail

Below are the details of the code used in this tutorial with proper explanation.

Controller : session_tutorial.php

copy the below code in your controller.

Class Session_Tutorial extends CI_Controller {

public function __construct() {

// Load form helper library

// Load session library

// Load validation library

// Show session demo page
public function session_demo_page_show() {

// Set values in session
public function set_session_value() {

// Check input validation
$this->form_validation->set_rules('session_value', 'Session Value', 'trim|required|xss_clean');

if ($this->form_validation->run() == FALSE) {
} else {

// Create session array
$sess_array = array(
'set_value' => $this->input->post('session_value')

// Add user value in session
$this->session->set_userdata('session_data', $sess_array);
$data['message_display'] = 'Value Successfully Set in Session';
$this->load->view('session_view', $data);

// Read session values
public function read_session_value() {

// Read all session values
$set_data = $this->session->all_userdata();

if (isset($set_data['session_data']) && $set_data['session_data']['set_value'] != NULL) {
$data['read_set_value'] = 'Set Value : ' . $set_data['session_data']['set_value'];
} else {
$data['read_set_value'] = 'Please Set Session Value First !';
$this->load->view('session_view', $data);

// Unset set values from session
public function unset_session_value() {
$sess_array = array(
'set_value' => ''

// Removing values from session
$this->session->unset_userdata('session_data', $sess_array);
$data['message_display'] = 'Successfully Unset Session Set Value';
$this->load->view('session_view', $data);


views : session_view.php

copy the below code in your view.

<title>Session View Page</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link href='|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>

<div id="main">
<div id="note"><span><b>Note : </b></span> In this DEMO we gives you functionality to set your own session value. </div>
<div class="message">
if (isset($read_set_value)) {
echo $read_set_value;
if (isset($message_display)) {
echo $message_display;

<div id="show_form">
<h2>CodeIgniter Session Demo</h2>
<?php echo form_open('session_tutorial/set_session_value'); ?>
<div class='error_msg'>
<?php echo validation_errors(); ?>
<?php echo form_label('Enter a session value :');?>
<input type="text" name="session_value" />
<input type="submit" value="Set Session Value" id='set_button'/>
<?php echo form_close(); ?>

<?php echo form_open('session_tutorial/read_session_value'); ?>
<input type="submit" value="Read Session Value" id="read_button" />
<?php echo form_close(); ?>

<?php echo form_open('session_tutorial/unset_session_value'); ?>
<input type="submit" value="Unset Session Value" id="unset_button" />
<?php echo form_close(); ?>


CSS : style.css

Styling HTML Elements.

margin:50px auto;
left: 246px;
top: 81px;
background-color: #FEFFED;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 30px;
float: left;
border-radius: 10px;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin-top: 70px;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
background-color: #FEFFED;
width: 100%;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
border-radius: 5px;
margin-bottom: 15px;
position: absolute;
font-weight: bold;
font-size: 28px;
left: 862px;
width: 500px;
text-align: center;
font-size: 16px;



So, this was all about creating and maintaining session using CodeIgniter framework. keep following us to learn more.