CodeIgniter 4 Session Library or Service

Share this Article
Reading Time: 6 minutes
642 Views

In any application, maintaining user session data and it’s activity is very important. It keeps track of every operation performed by user. By the help of session library or driver we can actually manage user session.

To use CodeIgniter 4 session library, we have some different approach from older versions, the Session library which is a class that permits us to maintain a user’s state and it’s data and track their activity while they browse any site.

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s see about CodeIgniter 4 Session library in detailed concept.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


How to use a Session Class?

When we define a session variable or it’s value, it will be typically run globally with each page load. So the Session class should be magically initialized so that we can access that variable and it’s value we can access anywhere.

To initialize the session:

$session = \Config\Services::session($config);

$config is an optional parameter. We can pass some configuration while initialization. Once we done the load, we can access $session anywhere in the application.

Alternatively, we can load session like this $session = session(); We can say it’s a shorthand way to load.

Session class we can find at this path – /vendor/codeigniter4/framework/system/Session/Session.php


What is Session data?

Session data means the key value pairs which we store into session to maintain user state across application. In Core PHP we usually use $_SESSION[‘key’] = ‘value’;

Here, inside this CodeIgniter 4, we will use few methods available which helps to store data, retrieve data, remove data etc.


Session Methods of CodeIgniter 4

As we already discussed that by the help of session class library, we do all operations like store, get, remove, destroy etc.

To accomplish the following tasks, we have several methods available in Session class library which reduces effort and do easily. Here are the following methods –

  • $session->set(“key”, “value”); To store value in session. Value will be either a single value or it can be an array.
  • $session->push(“key”, “newvalue”); To add new value to specified key. It key will be an array variable then it adds the new value to array.
  • $session->get(“key”); OR session(“key”); [ This is by using session helper function ] OR $session->key to get session value on the basis of specified key.
  • $session->remove(“key”); It removes the key what we have put here from session data.
  • $session->destroy(); This method destroy the complete session data from session what we have stored.
  • $session->has(“key”); To check session key exists or not.
  • $session->setFlashdata(“key”, “value”); To store flashdata means temporary data into session. It’s a one time use data only.
  • $session->getFlashdata(“key”); To get a stored flash data from session. Instead of using this way also we have an alternative option by using helper function of session session()->get(“key”);

Let’s use the session methods.


Usage of CodeIgniter 4 Session Library

We can use session methods in controller.

Learn More –

Session Initialization

We can load or create a session instance directly into any methods or best way to do in constructor of class.

$session = \Config\Services::session();

OR

$session = session();

Store Value in Session

# Single value
$session->set("username", "onlinewebtutorblog");

# Multiple values
$session->set("userdata", array(
   "name" => "Sanjay",
   "emp_id" => 1001,
   "email" => "sanjay_test@sample.com"
 ));

Read Session Value

$session->get("username");

OR

$session->username;

OR

session("username");

Remove Session Value

# Single value
$session->remove("username");

OR

# Complete session value
$session->destroy();

Store a Flash message

$session->setFlashdata("success", "Post has been added successfully");

Display a Flash message

session()->get("success");

Additionally, we can change session preferences or session settings at /app/Config/App.php. Configuration includes sessionDriver, sessionCookieName, sessionExpiration, sessionMatchIP etc.

To learn more about CodeIgniter 4 Session library also you can go through this official document Click here. We have an article which helps to prevent from Session hijack attacks Click here to go.

We hope this article helped you to fix CodeIgniter 4 Session Library in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment