How to use the session in CodeIgniter 4?

The session is used to track the status of the user as well as it is also used to set and get data and use it on different pages.session in php

Let’s see how to use the session in CodeIgniter 4 ?

Step 1 To use the session, firstly we need to access and initialize the session. There are two way to initialize session.

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

or

$session = session();

Step 2 Set data in the session.

 $session->set('session-name', session-value']);

Example:-

 $session->set('shopname_in_session', $_GET['username']);    

Step 3 How to get data from session? There are 3 ways to get data from session you can use any one ?

$name = $_SESSION['name'];

// or

$name = $session->name

// or

$name = $session->get('name');

session in phpexpoundsofttechsolution