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

Laravel vs Node Js: Which One Is Good For What?

Introduction In the world of web development, selecting the correct framework is critical. Laravel and…

3 months ago

Docker Cheatsheet: Essential Commands and Explanations

Introduction By enabling containerization, Docker has transformed the way software is built, deployed, and managed.…

8 months ago

Difference between Memcached and REDIS – Secrets of Caching

Introduction Speed and efficiency are critical in the ever-changing world of web development. Effective caching…

8 months ago

How to Revert a Git Commit: A Simple Example

Introduction Git, a popular version control system in software development, enables developers to track changes,…

8 months ago

What is Git Stash and Why Do You Need It

Introduction Are you tired of grappling with tangled changes and unfinished work while using Git?…

8 months ago

Difference between git stash pop and git stash apply

Introduction Version control is essential in software development because it ensures that changes to a…

8 months ago