PHP & Frameworks

How to set Session in CodeIgniter with basic example

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();
 
        $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

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)

Conclusion

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.

Developer Diary

Share
Published by
Developer Diary

Recent Posts

Git Tag Cheat Sheet

Introduction Git tags are an essential feature of version control systems, offering a simple way…

2 months ago

Understanding Web Storage: Cookies, Local Storage

Introduction The methods that browsers employ to store data on a user's device are referred…

3 months ago

Setting up OpenVPN Access Server in Amazon VPC – AWS

Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…

3 months ago

Enhance Error Tracking & Monitoring: Integrate Sentry with Node.js & Express.js

Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…

3 months ago

Comparing Callbacks, Promises, and Async/Await in JavaScript

Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…

5 months ago

How To Secure Nginx with Let’s Encrypt on Ubuntu EC2 Instance

Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…

7 months ago