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.
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.
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
The Requested Url /Phpmyadmin/ Was Not Found On This Server
MYSQL Dump File Using Command Prompt ?
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.
Introduction Git tags are an essential feature of version control systems, offering a simple way…
Introduction The methods that browsers employ to store data on a user's device are referred…
Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…
Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…
Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…
Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…