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.
  • 2. Removing Session Data.
  • 3. Flashdata.
  • 4. Tempdata.
  • 5. Destroying a Session.
  • 6. Accessing session metadata.

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.

<html>
<head>      
<title>CodeIgniter Session Example</title> 
</head>	
<body>
<?php echo $this->session->userdata('name'); ?>
<br><br>
<form action="<?php echo base_url(); ?>index.php/library/session_controller/retrieveSession">
<button>
Click Here</button> to set session data.
</form>

Output will be look like this

Codeigniter Session Class Library

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.

<html>
<head>      
<title>CodeIgniter Session Example</title> 
</head>	
<body>
<?php echo $this->session->userdata('name'); ?>
<br><br>
<form action="<?php echo base_url(); ?>index.php/library/session_controller/retrieveSession">
<button>
Click Here</button> To set session data.
</form>
<form action="<?php echo base_url(); ?>index.php/library/session_controller/un_set">   	
<button>Click Here</button> To unset session data.
</form>		 
</body>	
</html>

After click the unset button data will be automatically unset.

Output will be look like this

Codeigniter Session Class Library

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

Codeigniter Session Class Library

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

Codeigniter Session Class Library

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

Codeigniter Session Class Library

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. 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])
    
    • Parameters :
    • $key (mixed) : Session item key or NULL
    • Returns : Value of the specified item key, or an array of all userdata
    • Returns type : Mixed

    2. All userdata.

    This function return an array containing all userdata item.

    all_userdata()
    
    • Parameters :
    • Returns : An array of all userdata
    • Returns type : Array

    3. Get userdata.

    This function return a reference to the $_session array .

    &get_userdata()
    
    • Parameters :
    • Returns : A reference to $_SESSION
    • Returns type : Array

    4. Has userdata.

    This function check if an item exits in $_session.

    has_userdata($key)
    
    • Parameters :
    • $key (string) : Session item key
    • Returns : TRUE if the specified key exists, FALSE if not
    • Returns type : Bool

    5. Set userdata.

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

    set_userdata($data[$value = NULL])
    
    • Parameters :
    • $data (mixed) : An array of key/value pairs to set as session data, or the key for a single item
    • $value (mixed) : The value to set for a specific session item, if $data is a key
    • Returns type : Void

    6. Unset userdata.

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

    unset_userdata($key)
    
    • Parameters :
    • $key (mixed) : Key for the session data item to unset, or an array of multiple keys.
    • Returns type : Void

    7. Mark as flash.

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

    mark_as_flash($key)
    
    • Parameters :
    • $key (mixed) : Key to mark as flashdata, or an array of multiple keys.
    • Returns : TRUE on success, FALSE on failure
    • Returns type : Bool

    8. Get flash keys.

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

    get_flash_keys()
    
    • Parameters :
    • Returns : Array containing the keys of all “flashdata” items
    • Returns type : Array

    9. Unmark flash.

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

    get_flash_keys()
    
    • Parameters :
    • Returns : Array containing the keys of all “flashdata” items
    • Returns type : Array

    10. Flashdata.

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

    flashdata([$key = NULL])
    
    • Parameters :
    • $key (mixed) : Flashdata item key or NULL
    • Returns : Value of the specified item key, or an array of all flashdata
    • Returns type : Mixed

    11. Keep flashdata.

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

    keep_flashdata($key)
    
    • Parameters :
    • $key (mixed) : Flashdata key to keep, or an array of multiple keys
    • Returns : TRUE on success, FALSE on failure.
    • Returns type : Bool

    12. Set flashdata.

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

    set_flashdata($data[$value = NULL])
    
    • Parameters :
    • $data (mixed) : An array of key/value pairs to set as flashdata, or the key for a single item
    • $value (mixed) : The value to set for a specific session item, if $data is a key
    • Returns type : void

    13. Mark as temp.

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

    mark_as_temp($key[$ttl = 300])
    
    • Parameters :
    • $key (mixed) : Key to mark as tempdata, or an array of multiple keys
    • $ttl (int) : Time-to-live value for the tempdata, in seconds
    • Returns : TRUE on success, FALSE on failure
    • Returns type : Bool

    14. Get temp keys.

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

    get_temp_keys()
    
    • Parameters :
    • Returns : Array containing the keys of all “tempdata” items.
    • Returns type : Array

    15. Unmark temp.

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

    unmark_temp($key)
    
    • Parameters :
    • $key (mixed) : Key to be un-marked as tempdata, or an array of multiple keys
    • Returns type : Void

    16. Tempdata.

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

    tempdata([$key = NULL])
    
    • Parameters :
    • $key (mixed) : Tempdata item key or NULL
    • Returns : Value of the specified item key, or an array of all tempdata
    • Returns type : Mixed

    17. Set tempdata.

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

    set_tempdata($data[$value = NULL])
    
    • Parameters :
    • $data (mixed) : An array of key/value pairs to set as tempdata, or the key for a single item
    • $value (mixed) : The value to set for a specific session item, if $data is a key
    • $ttl (int) : Time-to-live value for the tempdata item(s), in seconds
    • Returns type : Void

    18. Sess regenerate.

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

    sess_regenerate([$destroy = FALSE])
    
    • Parameters :
    • $destroy (bool) : Whether to destroy session data
    • Returns type : Void

    19. Sess destroy.

    This function is used to destroy the current session.

    sess_destroy()
    
    • Parameters :
    • Returns type : Void

    20. Get.

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

    __get($key)
    
    • Parameters :
    • $key (string) : Session item key
    • Returns : The requested session data item, or NULL if it doesn’t exist
    • Returns type : Mixed

    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)
    
    • Parameters :
    • $key (string) : Session item key
    • $value (mixed) : Value to assign to the session item key
    • Returns type : Void

    Advertisements

    Add Comment

    📖 Read More