Tutorialsplane

Codeigniter Session Class Library


Codeigniter Session Class Library -We can load session class library like this $this->load->library(‘session’);. This library provide various functions that are used to maintain a user’s “state” and track thier activity while they browse your site. It has some session storage driver like file, database, redis, memcached. Here in this tutorial, we are going to explain how to use session class library.


Codeigniter session class library | Example

Let us understand how session class library works in codeigniter with examples.

Load session class library

First load session class library to use its functions, this library can be loaded simply as below-

How to load session class library:

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

Functions:-

There are following functions available in session class library. Now we will explain one by one.

1. Retrieving or add Session Data.

Here is simple demo of retrieving or add session data.

Controller Code:-

Syntax of retrieving or add session data.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class session_controller extends CI_Controller 
{
public function retrieveSession()
{
$this-?>load->library('session');
$this->session->set_userdata('name','Welcome To Tutorialsplane');	
$this->load->view('session_view');		
}
}
?>

View Code:-

Syntax of retrieving session data on view page.


      
<title>CodeIgniter Session Example</title> 
	

<?php echo $this-?>session->userdata('name'); ?>
<br/><br/>

Output will be look like this

2. Removing Session Data.

Here is simple demo of Removing Session Data.

Controller Code:-

Syntax of Removing Session Data.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class session_controller extends CI_Controller 
{
public function un_set()
{
$this-?>load->library('session');
$this->session->unset_userdata('name'); 
$this->load->view('library/session_view');
}
}
?>

View Code:-

Syntax of Removing Session Data on view page.


      
<title>CodeIgniter Session Example</title> 
	

<?php echo $this-?>session->userdata('name'); ?>
<br/><br/>

After click the unset button data will be automatically unset.

Output will be look like this

3. Flashdata.

Here is simple demo of Flashdata.

Controller Code:-

Syntax of Flashdata.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class session_controller extends CI_Controller 
{
public function flash()
{
$this-?>load->library('session');
$this->load->helper('url'); 
$this->session->set_flashdata('item','Welcome to solid coupon');
echo $this->session->flashdata('item');
}
}
?>

Output will be look like this

4. Tempdata.

Here is simple demo of tempdata.

Controller Code:-

Syntax of tempdata example.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class session_controller extends CI_Controller 
{
public function tempdata()
{
$this-?>load->library('session');
$this->load->helper('url'); 
$this->session->set_tempdata('Company name','SolidCoupon', 240);
echo $this->session->tempdata('Company name');
}
}
?>

Output will be look like this

5. Destroying a Session.

Here is simple demo of destroying a session.

Controller Code:-

Syntax of destroying a session.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class session_controller extends CI_Controller 
{
public function destroy()	
{
$this-?>session->sess_destroy();
redirect(base_url());
}
}
?>

Output will be look like this

6. Accessing session metadata.

This session library comes with four session driver.

  1. Files Driver.
  2. Database Driver.
  3. Redis Driver.
  4. Memcached Driver.
  5. 0l>

    Files Driver.

    This file driver use your file system for storing session data.

    You should do something like this:-

    mkdir /path to directory/sessions/
    chmod 0700 /path to directory/sessions/
    chown www-data /path to directory/sessions/

    Database Driver.

    The database driver use a relational database such as MYSQL and PostgreSQL to store session.

    You should do something like this:-

    $config['sess_driver'] = 'database';
    $config['sess_save_path'] = 'ci_sessions';
    

    Redis Driver.

    Redis driver is a storage engine which is used for caching and popular because for high performance.

    You should do something like this:-

    $config['sess_driver'] = 'redis';
    $config['sess_save_path'] = 'tcp://localhost:6379';
    

    Memcached Driver.

    This driver is similar to ‘redis’ one in all of its properties, except perhaps for availability.

    You should do something like this:-

    $config['sess_driver'] = 'memcached';
    $config['sess_save_path'] = 'localhost:11211';
    

    Class reference:-

    There are following references available in session class library. Now we will explain.

    1. Userdata.

    This reference is used to get a value for a specific session item.

    userdata([$key = NULL])
    

2. All userdata.

This function return an array containing all userdata item.

all_userdata()

3. Get userdata.

This function return a reference to the $_session array .

&get_userdata()

4. Has userdata.

This function check if an item exits in $_session.

has_userdata($key)

5. Set userdata.

This method is used to assign data to the $_session super global.

set_userdata($data[$value = NULL])

6. Unset userdata.

This method is used to unset the key from the $_session super global.

unset_userdata($key)

7. Mark as flash.

This method is used for mark a $_session item key(multiple key) as flashdata.

mark_as_flash($key)

8. Get flash keys.

This function get a list of all $_session which is marked as flashdata.

get_flash_keys()

9. Unmark flash.

This function unmark a $_session item key(Multiple one) as flashdata.

get_flash_keys()

10. Flashdata.

This method is used to get a specific $_session item that has been marked as flashdata.

flashdata([$key = NULL])

11. Keep flashdata.

This method is used to get a specific session data key as flashdata through next request.

keep_flashdata($key)

12. Set flashdata.

This method is used to assign data to the $_SESSION superglobal and marked as flashdata.

set_flashdata($data[$value = NULL])

13. Mark as temp.

This function is used to marked a session item key(Multiple one) as tempdata.

mark_as_temp($key[$ttl = 300])

14. Get temp keys.

This function get a list of all $_session which is marked as tempdata.

get_temp_keys()

15. Unmark temp.

This function unmarks a $_SESSION item key (multiple one) as tempdata.

unmark_temp($key)

16. Tempdata.

This function is used to get the value for a specific $_SESSION item which is marked as tempdata.

tempdata([$key = NULL])

17. Set tempdata.

This function is used to assign data to $_session superglobal and marked as tempdata .

set_tempdata($data[$value = NULL])

18. Sess regenerate.

This is used to regenerate session ID and optionally destroying the current session data.

sess_regenerate([$destroy = FALSE])

19. Sess destroy.

This function is used to destroy the current session.

sess_destroy()

20. Get.

This is a magic function that allows to return the session id by calling session_id().

__get($key)

21. Set.

This is also a magic function that allows you to assign items to $_SESSION by accessing them as $this->session properties.

__set($key, $value)

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference