Codeigniter - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Session

CodeIgniter Session

shape Description

CodeIgniter Session gives the information about the condition of the client and the track the user information when they visit the website. If a request is made by the user to the server, a session creates automatically in the server to track the requests from the user. A session will run after every page load globally.

Load Session

shape Description

CodeIgniter session class will manage and stores session data of each user. A session library can be loaded in two ways: Method-1 : Load the session by adding the below statement in Controller constructor. [php]$this->load->library('session');[/php] Method-2 : Load the session using the file autoload.php present in the filepath applicatio/config in Controller constructor by adding the below code. [php]$autoload['libraries'] = array('session');[/php] Note: Normally, CodeIgniter Session class won't use default PHP sessions because session class creates its own session data.

Operations on Session

shape Description

Add Session data

The data to the session can be added using the method set_userdata(). Every user data can be added individually or can be grouped into an associative array. [php]$this->session->set_userdata($array); // $array is an associative array of your new data[/php]

Retrieve Session data

Session data can read and retrieve the data using the method userdata() with array index as the value. [php]$this->session->userdata('item'); // Where item is the array index like session id[/php]

Retrieve All Session data

All the data of the session can be retrieved using the method all_userdata() [php]$this->session->all_userdata() // Read all session values[/php]

Remove Session data

All the session data can be removed using the method unset_userdata. [php]$this->session->unset_userdata('some_name'); // Removing values from session[/php]

Destroy Session

The method sess_destory() will completely destroy the session in the application. [php]$this->session->sess_destroy();[/php]

shape Example

[php] <?php defined('BASEPATH') OR exit('No direct script access allowed'); class MyController extends CI_Controller { public function session1() { $this->session->set_userdata('test','CodeIgniter'); } public function getsession() { echo $this->session->userdata('test'); } public function session2() { $data = array('sno'=>1,'name'=>'vikram','course'=>'CodeIgniter'); $this->session->set_userdata($data); } public function getsession2() { echo $this->session->userdata('sno') . '<br>'; echo $this->session->userdata('name') . '<br>'; echo $this->session->userdata('course') . '<br>'; } public function uset() { $this->session->unset_userdata('sno'); $this->session->unset_userdata('name'); } public function clearsession() { $this->session->sess_destroy(); } } [/php] In the above code, "test" is the method which stores the individual value 'CodeIgniter' in the session.

Summary

shape Key Points

  • CodeIgniter Session gives the information about the condition of the client and the track the user information when they visit the website.
  • set_userdata() adds the session data to the application.