How To Set and Get Session In CodeIgniter 4
In today's article we are going to learn some new stuff about codeIgniter 4 that how we can set a session and how we can retrieve a value from sessio...
In today's article we are going to learn some new stuff about codeIgniter 4 that how we can set a session and how we can retrieve a value from session.
Pretty simple to use session in CodeIgniter and let's start it from scratch. Maybe you are a newbie and don't know about the session so, I want to discuss a few things about session.
What's A Session
A session used to store the value of anything it can be a variable value or anything which you can use on your browser in that particular application. A session alive until you logged out or cut out from your application. With session you can access the data across all your application pages. It's a global variable. And normally we do store a session using below syntax.
$_SESSION['session_name'] = 'value';
Notice: cookie is a different thing but also used to store the information.
Session In CodeIgniter4
Well, we are talking about the session in codeIgniter 4. It provides session library. That helps you to get and set the session variable in very convenient way. Well, It's not bad haa. Because when you do use codeIgniter 4 's session methods then it looks very easy and easy to type, memorable anytime.
It provides you a session library that we need to initialize before using it. Well, One TIP I would love to give that whenever you will be using the session library then you shouldn't initialize it every time go to basecontroller and initialize it there so that you can use session in each controller without having initializing it everytime.
Library Initialization as described below:
// In base Controller
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
// Preload any models, libraries, etc, here.
$this->session = \Config\Services::session();
}
Set Session In CodeIgniter 4
And now move to main stuff of setting a session. Now I am assuming that you know how to initialize a codeigniter 4 session library. And now use the variable to set the session as described in the code below.
$this->session->set([
'name' => 'Robin',
'class' => '12th',
'college' => 'No',
]);
Get Session Value In CodeIgniter 4
Getting a session value is the same as we have set. Because now you just need to pass the key that you have assigned to particular session variable. As you can see above I have assigned three values and all three values has keys. Now we will get the session using get(_) method.
As described below:
$this->session->get('name'); //output = Robin
Destroy A Session In CodeIgniter 4
We have covered all the points now but the main thing that we used to destroy a session is using delete() method. Yes, If you want to delete any session data you can either pass a single key name or an array as well. Removing a session variable described below in the code:
$this->session->remove('name');
I hope this will help you. You can also read about some advance stuff about CI session on docs as well.