Session in CodeIgniter

Session in CodeIgniter

We know that HTTP is a stateless protocol, which means that every HTTP requests are independent and it cannot maintain the user information or app state. So any data set by the user on a page cannot be retrieved on another page.

What is session?

Session is a way to store information so that user data can be retrieved on multiple pages of the application. Session data is stored on user basis so that the data is available only for the same user. Unlike Cookies session data is stored in the server.

Advantages and disadvantages

Session is the simplest and most effective way to maintain the user data within the application. It is easy to implement. The biggest advantage of session is that it can store any type of object.

Since session data is stored in the server memory, a large number of users or data may eat up the server memory and affect the performance. Another disadvantage of the session is that, unless explicitly specified, cookies will be used to store user information. If the user has disabled cookies for some reason, the application may not be able to take advantage of sessions. In such cases, cookie-less session should be used.

Session management in CodeIgniter

CodeIgniter comes with a built in session handling library to help in session management. To use the session class, it should be either specified in the constructor of the controller or it should be auto-loaded.

To add session class in a Controller, add the following code in the constructor of the Controller.

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

Setting session data

Session data can be retrieved either by using the classic PHP $_SESSION superglobal as:

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

Or by using CodeIgniter set_userdata() method.

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

You can also pass an array to the method as:

$data = array(
         'username'  => 'johndoe',
         'role'     => 'user');
$this->session->set_userdata($data);

Get session data

Session data can be retrieved either by using the classic PHP $_SESSION superglobal:

$value=$_SESSION['key'];

or by using the CodeIgniter session library as:

$value=$this->session->key;

If you want to ensure backward comparability, use userdata() method.

$value=$this->session->userdata('key');

Checking session data

To check if a session variable exists, either use PHP’s built-in isset() method or use CodeIgniter has_userdata() method.

if(isset($_SESSION['key']))
{
    //do your magic here
}

//Or use has_userdata() method
if($this->session->has_userdata('key'))
{
    //do your magic here
}

Flashdata

Flashdata is simply some session data that is available only for one successive request. To set a flashdata, use set_flashdata() method.

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

You can also pass an array to the method if you have multiple flashdata data to store.

Also a session data can be marked as flashdata using mark_as_flash() method.

$_SESSION['key'] = 'value';
$this->session->mark_as_flash('key'); 

Flashdata is available only for the next request and it cannot be retrieved using userdata() method.

To retrieve flashdata, use flashdata() method. A call to this method without parameters will return an array of flashdata.

$value=$this->session->flashdata('key');

Tempdata

Tempdata is similar to flashdata. The only difference between these two is that unlike flashdata that expires after the next request, tempdata expires only after a specific period of time.

Unlike flashdata, tempdata expires after a specific period of time.

To set tempdata, use set_tempdata() method.

 $this->session->set_tempdata('key', 'value', expiration_time_in_seconds); 

You can also pass an array of elements to the method as:

$data = array('username' => 'johndoe', 'role' => 'user');
$this->session->set_tempdata($data, NULL, 200); 

Individual items can be set using mark_as_temp() method.

$_SESSION['key'] = 'value';
$this->session->mark_as_temp('key', 60);
//Expires after 1 minute.

Multiple items can be marked as tempdata as:

$this->session->mark_as_temp(array(
    'item1'  => 60,
    'item2' => 120)); 
//60 and 120 is the expiration time.

Share Tweet Send
Loading...
You've successfully subscribed to Geekinsta
Great! Next, complete checkout for full access to Geekinsta
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.