In other words, the state of the application is maintained. That is to say if when you logged in you set the company name to a global variable then even after you close the login form that state of the company name is preserved. HTTP works a bit different from the above scenario that we have just described. It is stateless. That is that say whatever you do in one request does not persevere in the next request. T o work around this problem. We have two (2) solutions in PHP. We can either work with cookies which are small files placed on the user’s computer or work with sessions which are similar to cookies but are instead stored on the server and have a bigger capacity than cookies. In this tutorial, you will learn-

CodeIgniter Session Management
When to use sessions
Sending Flash Messages to other pages with CI Sessions
Storing User Data in CI Sessions

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

In this section, you will learn about sending flash messages to other pages using the session library in CodeIgniter Create a new file SessionController in Add the following code HERE,

class SessionController extends CI_Controller {…} defines the SessionController class and extends the parent controller class. public function __construct() {…} defines the constructor method that initializes the parent class, and loads the url helper and session library. public function index() {…} defines the session index method that loads the session index view. public function flash_message(){…} defines the flash message method which sets the flash data then redirects to the flash_index route

Let’s now create the view that will display the value of the session data. Create a new directory session in application/views Create a new file index.php in application/views/sessions Add the following code HERE,

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 Open application/config/routes.php Add the following lines Let’s now start the built-in server for PHP and test our flash messages Open the terminal Run the following command HERE, The above command browses to the application code directory and starts the built-in server on port 3000. Note: the application path has to match the path where you downloaded CodeIgniter, and you can use any port number that is free on your computer. It’s not necessary to use port 3000. Load the following URL in your web browser: http://localhost:3000/flash_message You will be redirected to the following URL, and you will get the following results: http://localhost:3000/flash_index

Click on the refresh button of your web browser or press F5 You will now get the following results

Storing User Data in CI Sessions

Let’s now look at a slightly more advanced example. To make it simple, we will simulate user authentication and not do the actual implementation of verifying the user record in the database and the submitted password. Let’s start with the routes Open routes.php located in application/config Add the following routes HERE,

$route[‘login’] = ‘sessioncontroller/login’; defines the route that displays the login form $route[‘authenticate’] = ‘sessioncontroller/authenticate’; defines the route that simulates successful user login and sets the session login data. $route[‘settings’] = ‘sessioncontroller/settings’; defines a protected page that should only be accessible to logged in users $route[‘dashboard’] = ‘sessioncontroller/dashboard’; defines a protected page that should only be accessible to logged in users. $route[‘logout’] = ‘sessioncontroller/logout’; logs out the user by destroying the session data

Let’s now update the SessionController Open application/controllers/SessionController.php Add the following methods HERE,

public function check_auth($page) {…} defines the method that checks if the user is logged then allows access to the page. If a user is not logged in then the user is redirected to the login page with a flash message. public function login() {…} loads the login view located in sessions directory. public function authenticate() {…} sets the session user data for the keys logged_in and username. NOTE: We are not verifying any login details against the database. We are simply assuming the submitted details are ok and set the session data. public function dashboard() {…} loads the dashboard page after calling the $this->check_auth(‘dashboard’); which verifies that the logged_in session key is set. public function settings() {…} loads the settings page which is also protected public function logout() {…} destroys the session data and signs out the user. The method also redirects to the login page

CodeIgniter Session Views

Create the following views in application/views/sessions

dashboard.php login.php settings.php

Add the following code to dashboard.php HERE,

session->userdata(‘username’);?> displays the user name which we set in the authentication method

Add the following code to login.php HERE, The login form submits to authenticate route. Add the following code to settings.php That’s it for our views. Let’s now test our application. In this tutorial, we are using the built-in PHP web server, but you can use any web server that supports PHP. Open the terminal Run the following command HERE, The above command browses to the application code directory and starts the built-in server on port 3000. Note: the application path has to match the path where you downloaded CodeIgniter, and you can use any port number that is free on your computer. It’s not necessary to use port 3000. Open the following URL in your web browser: http://localhost:3000/dashboard you will be redirected to the following page

Click on the Login button You will see the following results

After we log in, we are now able to see the session data.

Summary

In this tutorial, you have learned the basics of CodeIgniter session library and learned how to use it to store temporal data as flash messages and how to use store more permanent data, i.e., user login data.