Codeigniter 4 Tutorials
- Codeigniter 4
- Application Run
- Application Structure
- Configuration files
- Errors
- Controller
- View
- Model
- How to remove index.php from URL
- Route
- CodeIgniter URLs
- Helper
- Session
- Global Functions and Constants
- Logging Information in CI4
- Error Handling
- Signin / SignUp
- Login / Register
- CRUD
- Crud 1
- Form Validation
- Data Table
- Pagination
- File Upload
- Show Data Using Ajax
- Select2 AJAX Autocomplete Search
- Curl Post
- Rest API
- Weblink
What is Session in codeigniter 4?
Session library is a class that maintain a user’s state and it’s data and track their activity while they browse any site.
How to use a Session Class?
When we define a session variable or it’s value, it will be run globally with each page load of your application.
How To initialize the session?
File name : index.php
$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.
How to create Session data?
Session data means the key value pairs which we store into session to maintain user state across application.
we have several methods available in Session class library
File name : index.php
$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”);
How do Sessions work?
When a page is loaded, the session class will check to see if a valid session cookie is sent by the user’s browser. If a sessions cookie does not exist, a new session will be created and saved.
If a valid session does exist, its information will be updated. With each update, the session ID may be regenerated if configured to do so.
File name : index.php
Session Initialization
We can load or create a session instance directly into any methods or best way to do in constructor of class.
File name : index.php
$session = \Config\Services::session();
OR
$session = session();
Store Value in Session
File name : index.php
# Single value
$session->set("username", "mahtab");
# Multiple values
$session->set("userdata", array(
"name" => "Mahtab",
"empid" => 101,
"email" => "mahtab@ittutorial.in"
));
$newdata = [
'username' => 'mahtab',
'email' => 'mahtab@ittutorial.in',
'logged_in' => TRUE
];
$session->set($newdata);
Read Session Value :-
File name : index.php
$session->get("username");
OR
$session->username;
OR
session("username");
Remove Session Value
File name : index.php
# Single value
$session->remove("username");
OR
# Complete session value
$session->destroy();
Store a Flash message
File name : index.php
$session->setFlashdata("success", "Post has been added successfully");
Display a Flash message
File name : index.php
session()->get("success");
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php
File name : index.php