Skip to content

How to set Session in CodeIgniter with basic example

set ci session

If you create a website in Codeigniter and use a login system, you need to understand how to set up the session in Codeigniter. But the session using HTTP and HTTP is stateless. That means whatever you do on one request doesn’t stop on the next request. To avoid this problem. We have two solutions in PHP.

We can work with cookies, which are small files placed on the user’s computer, or work with sessions, which are similar to cookies but are stored on the server and have a larger capacity than cookies.

When to use Set sessions In codeIgniter?

Sessions are often useful when you want to know the user’s activities from one page to another. For example, let’s say you have a protected area on the website. Users don’t have to log in to every page. The user logs in once and stores their details in a session variable, and then reuses the same data in subsequent requests. Other use cases are when working in a purchasing system and the user needs to add items to the shopping cart.

Alternatively, CodeIgniter also uses sessions, so the data is only available once on the next request. This is useful when you have an edited and updated database record and want to provide 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();
    public function index() {
    public function message(){
        $this->session->set_flashdata('msg', 'Welcome to CodeIgniter Flash Messages');

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

        <title>Code Igniter Flash Session</title>
        <p>The session value of msg is <b> <?=$this->session->userdata('msg');?> </b></p>

<?=$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
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

related Articles

The Requested Url /Phpmyadmin/ Was Not Found On This Server

MYSQL Dump File Using Command Prompt ?

PhpMyAdmin Issue With 7.2 [Sql.Lib.Php]

Why All Sites Now Require SSL (Https)


In the this article or blog, we learned how to set up a session in CodeIgniter. This question is most easily googled by a PHP programmer who wants to learn how to code CodeIgniter or PHP. any login or data movement throughout the site.