onlinecode

CodeIgniter Session Data Demo And example

CodeIgniter Session Data Demo And example

CodeIgniter Session Data is essentially an exhibit related with a specific CodeIgniter session ID (treat). On the off chance that you’ve utilized sessions in PHP some time recently, you ought to be acquainted with PHP’s $_SESSION superglobal (if not, please read the substance on that connection).

CodeIgniter offers access to its session Data through similar means, as it uses the CodeIgniter session handlers’ instrument given by PHP. Utilizing session Data is as straightforward as controlling (perused, set and unset values) the $_SESSION exhibit.

Furthermore, CodeIgniter additionally gives 2 extraordinary sorts of CodeIgniter session Data that are additionally clarified beneath: flashdata and tempdata.

Note :: In past variants, customary CodeIgniter session Data in CodeIgniter was alluded to as ‘userdata’. Have this at the top of the priority list if that term is utilized somewhere else in the manual. A large portion of it is composed to clarify how the custom “userdata” strategies work.

Recovering CodeIgniter Session Data

Any snippet of data from the CodeIgniter session exhibit is accessible through the $_SESSION superglobal:

$_SESSION['item']
//Or through the magic getter:

$this->session->item

What’s more, for in reverse similarity, through the userdata() strategy:

$this->session->userdata('item');

Where thing is the exhibit key comparing to the thing you wish to bring. For instance, to allot a formerly put away “name” thing to the $name variable, you will do this:

$sessionname = $_SESSION['name'];

// or:

$sessionname = $this->session->name

// or:

$sessionname = $this->session->userdata('name');

Note :: The userdata() strategy returns NULL if the thing you are attempting to get to does not exist. In the event that you need to recover the greater part of the current userdata, you can just preclude the thing key (enchantment getter works for properties):

$_SESSION

// or:

$this->session->userdata();

Including CodeIgniter Session Data

Suppose a specific client sign into your site. Once confirmed, you could include their username and email deliver to the CodeIgniter session, making that Data comprehensively accessible to you without running a database question when you require it.

You can just dole out Data to the $_SESSION cluster, as with whatever other variable. Or, then again as a property of $this->session.

On the other hand, the old technique for allotting it as “userdata” is additionally accessible. That however passing a cluster containing your new Data to the set_userdata() strategy:

$this->session->set_userdata($array);

Where $array is a cooperative exhibit containing your new Data. Here’s an illustration:

$set_data = array(
'username'  => 'onlinecode',
'email'     => 'ingo@onlinecode',
'is_loggedin' => TRUE
);

$this->session->set_userdata($set_data);

In the event that you need to include userdata one incentive at any given moment, set_userdata() additionally underpins this linguistic structure:

$this->session->set_userdata('session_name', 'session_value');

On the off chance that you need to confirm that a CodeIgniter session esteem exists, just check with isset():

//returns FALSE if the 'session_name' item doesn't exist or is NULL,
// otherwise TRUE set ::
isset($_SESSION['session_name'])

Or, on the other hand you can call has_userdata():

$this->session->has_userdata('session_name');

Expelling CodeIgniter Session Data

Similarly as with some other variable, unsetting an incentive in $_SESSION should be possible through unset():

unset($_SESSION['session_name']);

// or multiple values:

unset(
$_SESSION['session_name'],
$_SESSION['session_another_name']
);

Likewise, similarly as set_userdata() can be utilized to add data to a CodeIgniter session, unset_userdata() can be utilized to expel it, by passing the CodeIgniter session key. For instance, in the event that you needed to expel session_name from your CodeIgniter session Data cluster:

$this->session->unset_userdata('session_name');

This strategy likewise acknowledges a variety of thing keys to unset:

$sessionarray_items = array('session_username', 'email');
$this->session->unset_userdata($sessionarray_items);

Fetch CodeIgniter Session Data

In the wake of setting Data in CodeIgniter session, we can likewise recover that Data as demonstrated as follows. Userdata() capacity will be utilized for this reason. This capacity will return NULL if the Data you are attempting to get to is not accessible.

$session_name = $this->session->userdata('your_name');

Case for Create a controller class called Example_Session_controller.php and spare it in application/controller/Example_Session_controller.php.

<?php
class Example_Session_controller extends CI_Controller
{
public function index()
{
//set loading session library
$this->load->library('session');

//session adding data
$this->session->set_userdata('session_name','onlinecode');

$this->load->view('session_view');
}

public function unset_session_data() {
//loading session library
$this->load->library('session');

//removing session data
$this->session->unset_userdata('session_name');
$this->load->view('session_view');
}

}
?>

Make a view document called session_view.php and spare it in application/sees/session_view.php

<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>CodeIgniter Session Example and demo - onlinecode</title>
</head>
<body>
Welcome user, <?php echo $this->session->userdata('session_name'); ?>
<br>
<a href = 'https://onlinecode.org/index.php/onlinecodedemo/index.php/onlinecodesession/unset'>
Click Here</a> to unset session data.
</body>
</html>

Roll out the improvements in the routes.php document in application/config/routes.php and include the accompanying line toward the finish of the record.

$route['onlinecodesession'] = 'Example_Session_controller';
$route['onlinecodesession/unset'] = "Example_Session_controller/unset_session_data";

Execute the above case by utilizing the accompanying location. Supplant onlinecode with the URL of your website.


You also like Angular Gauge chart and Angularjs Image Gallery and Angularjs pie chart and Vuejs WYSIWYG Editor and angular 2 gauge chart

Exit mobile version