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 –

Syntax:

$this->load->library('session');

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:

http://localhost/session/index.php/session_tutorial/session_demo_page_show

 


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.

<?php
Class Session_Tutorial extends CI_Controller {

public function __construct() {
parent::__construct();

// Load form helper library
$this->load->helper('form');

// Load session library
$this->load->library('session');

// Load validation library
$this->load->library('form_validation');
}

// Show session demo page
public function session_demo_page_show() {
$this->load->view('session_view');
}

// 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) {
$this->load->view('session_view');
} 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.

<html>
<head>
<title>Session View Page</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
</head>
<body>

<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">
<?php
if (isset($read_set_value)) {
echo $read_set_value;
}
if (isset($message_display)) {
echo $message_display;
}
?>
</div>

<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(); ?>
</div>
<?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(); ?>
</div>
</div>
</body>
</html>

 

CSS : style.css

Styling HTML Elements.

#main{
width:960px;
margin:50px auto;
font-family:raleway;
}
span{
color:red;
}
#note{
position:absolute;
left: 246px;
top: 81px;
}
h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 30px;
}
#show_form{
width:300px;
float: left;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin-top: 70px;
}
input[type=text]{
width:100%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
background-color: #FEFFED;
}
input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: 15px;
}
.message{
position: absolute;
font-weight: bold;
font-size: 28px;
top:150px;
left: 862px;
width: 500px;
text-align: center;
}
.error_msg{
color:red;
font-size: 16px;
}

 

Conclusion:

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