Skip to content

How to set Session in CodeIgniter with basic example

If you are making a website, and you are using a login system then you need session. But session using HTTP and HTTP is stateless. That is that say whatever you do in one request does not persevere in the next request. To work around this problem. We have two solutions in PHP. We can either work with cookies which are small files placed on the user’s computer or work with sessions that 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

Create a new file SessionController in application/controller/SessionController.php

Add following code in above created file

<?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 message(){
        $this->session->set_flashdata('msg', 'Welcome to CodeIgniter Flash Messages');
        redirect(base_url('flash_index'));
    }
}

The above message() function we are using to set the session message and redirect to flash_index routeLet’s now create the view that will display the value of the session data.Create a new directory session in application/views and Create a new file index.php in application/views/sessions

Add the following code in this file

<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>

<?=$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 and Open application/config/routes.php

Add the following lines

$route['flash_index'] = 'session_controller';
$route['message'] = 'session_controller/message';

Run the codeigniter application and check the result
Load this url http://localhost:3000/message
You will be redirected to the following URL, and you will get the  results in browser
http://localhost:3000/flash_index
Click on the refresh button of your web browser or press F5
You will now get the different result.
Hope this article will help you all