CodeIgniter Session Management
If you have developed desktop applications before then, you probably know that you can define a global variable assign a value to it and use it throughout the life cycle of the application opening and closing more than one (1) and each request will have access to the global variable.
In other words, the state of the application is maintained. That is to say if when you logged in you set the company name to a global variable then even after you close the login form that state of the company name is preserved.
HTTP works a bit different from the above scenario that we have just described. It is stateless. That is that say whatever you do in one request does not persevere in the next request. T
o work around this problem. We have two (2) solutions in PHP. We can either work with cookies which are small files placed on the user’s computer or work with sessions which are similar to cookies but are instead stored on the server and have a bigger capacity than cookies.
When to use sessions?
Sessions are usually useful when you want to know the user’s activities from page to page. For example, let’s say you have a protected area on the website. The users don’t need to login on each page. You can let the user login once and store their details in a session variable then reuse the same data on further requests. Other use cases include when working on a shopping system and the user has to add items to the shopping cart.
Alternatively, CodeIgniter also uses sessions to make data available only once on the next request. This is useful you have may be edited and updated a database record, and you want to return some feedback to the user when they are redirected to another page.
Sending Flash Messages to other pages with CI Sessions
In this section, you will learn about sending flash messages to other pages using the session library in CodeIgniter
Create a new file SessionController in
application/controllers/SessionController.php
Add the following code
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class SessionController extends CI_Controller { public function __construct() { parent:: __construct(); $this->load->helper('url'); $this->load->library('session'); } public function index() { $this->load->view('sessions/index'); } public function flash_message(){ $this->session->set_flashdata('msg', 'Welcome to CodeIgniter Flash Messages'); redirect(base_url('flash_index')); } }
HERE,
- class SessionController extends CI_Controller {…} defines the SessionController class and extends the parent controller class.
- public function __construct() {…} defines the constructor method that initializes the parent class, and loads the url helper and session library.
- public function index() {…} defines the session index method that loads the session index view.
- public function flash_message(){…} defines the flash message method which sets the flash data then redirects to the flash_index route
Let’s now create the view that will display the value of the session data.
Create a new directory session in application/views
Create a new file index.php in application/views/sessions
Add the following code
<html> <head> <title>Code Igniter Flash Session</title> </head> <body> <p>The session value of msg is <b> <?=$this->session->userdata('msg');?> </b></p> </body> </html>
HERE,
- <?=$this->session->userdata(‘msg’);?> retrieves the value of the session data with the key of msg and displays it in the browser.
Let’s now create the routes for our session flash method
Open application/config/routes.php
Add the following lines
$route['flash_index'] = 'session controller'; $route['flash_message'] = 'session controller/flash_message';
Let’s now start the built-in server for PHP and test our flash messages