Session management in CodeIgniter
×

Session management in CodeIgniter

2121

  • Sometimes we need to track user’s activity and state when they are browsing our application. We can do this with the help of session management.
  • Codeigniter provides an inbuilt class to maintain sessions.

Related Topics:

Codeigniter Interview Questions
Form Validation in Codeigniter
Sending Email using Codeigniter

Initializing a session:

We know that sessions are work in a global environment means we can access session data on every page of the application. To use the sessions, first of all, we have to initialize them.

Syntax:

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

After adding the above line into the controller, we can use the session object to create sessions as:

$this->session

Adding session data:

Session data is simply an associated array with a particular session ID. To set the data into sessions, we simply use PHP superglobal $_SESSION array.

$_SESSION[‘key’] = value;

Here,

  • Key is the key of the session array.
  • Value is the value that assigns to a given key.

In codeigniter, we use set_userdata() function to set the value of session variable.

Syntax:

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

Where name is the name of a variable and value is the value that assigns to the name variable.

In case to assign a whole array to a session, we use the following method.

$newdata = array(
'name' => 'Ram',
'class' => '11
);
$this->session->set_userdata($newdata);
Note

If you want to check whether a session value exists or not, simply use below isset() function or has_userdata() function:

isset($_SESSION['some_name'])
Or
$this->session->has_userdata('some_name');

Retrieving session data:

After creating a session, we can use it through the application.

We can fetch the session data by three approaches:

1 By using the Standard PHP method.

$name = $_SESSION[‘name’];

2 By using magic getter.

$name = $this->session->name

3 By using userdata() method.

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

If the item you are trying to access does not exist, the userdata() method returns NULL.

Remove Session Data:

We have two approaches to removing the data of sessions.

1 By using the simple PHP unset() method.

unset($_SESSION['some_name']);

// or multiple values:

unset(
$_SESSION['some_name'],
$_SESSION['another_name']
);

2 By using unset_userdata() method.

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

In the case of array use,

$this->session->unset_userdata($array_items);

Destroying a Session:

We have two approaches to clear the current session.

1 By using PHP destroy() method.

session_destroy();

2 By using sess_destroy() method.

$this->session->sess_destroy();
Example:

Step 1 Open the application/views directory and create a new view file session_view.php.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
<h2>Session management example</h2>

<h4> Welcome <?php echo $this->session->userdata('name'); ?> </h4>
<br>
<h5><a href = 'http://localhost/ci/index.php/Session_controller/ unset_session_data '>
Click Here</a> to unset session data.<h5>

</div>

</body>
</html>

Step 2 Open the application/controller directory and create a new controller Session_controller.php.

<?php
class Session_controller extends CI_Controller {

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

//adding data to session
$this->session->set_userdata('name','Pooja Chikara');

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

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

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

}
?>

Step 3 Create a new file unset_view.php in the application/views directory.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
<h2>Session management example</h2>
<h4>Session unset successfully.</h4>
</div>

</body>
</html>

Step 4 Open the given URL into the browser.

http://localhost/ci/index.php/Session_controller

After clicking on the click here link.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments