Table of contents
In this post, I will show you about the Laravel 8 session with examples and the importance of sessions in programming. A session is the most useful when creating a web application, and it will handle storing data without saving it on the database.
The session is used to protect user data who currently view your web application, and it will be secured because it cannot be updatable or writable. The session is assigned with a unique id which is used to get the stored values. Even if the session is created, a cookie stored a unique session saved on the visitor's computer and will retrieve every request to the server.
Â
Laravel framework provides easy ways to handle the session. Laravel sessions can be stored in databases, files, or encrypted cookies. Laravel session configuration can be found in config/session.php. By default, Laravel session data is stored in storage files. You can also use the database if you want to use it just change the driver in your .env file and run the following command below:
Â
php artisan session:table
php artisan migrate
Â
Once you run the above command your Laravel session will store in the sessions table.
Â
Storing Data in Laravel Session
Laravel provides a way of handling sessions. The first is to use the Request instance and the second uses the session() helper function.
// Request instance
$request->session()->put('key','value');
// global session helper
session(['key' => 'value']);
Â
Getting Data with Specified Key
If you want to get the session with your specific key use the following method below:
// Request instance
$value = $request->session()->get('key');
// global helper method
$value = session('key');
// return session with default value if the session key is not found
$value = session('key', 'default');
Â
If you need to display all data stored in the session, just run the following method.
$data = $request->session()->all();
Â
Checking session with key
To check if the specified key exists in the session, just run the following method.
if ($request->session()->has('users')) {
$user = $request->session()->get('users');
}
Â
To check if the specified key session is null, run the following method.
if ($request->session()->exists('users')) {
$user = $request->session()->get('users');
}
Â
Push Array Session Value
Laravel also provides a method to push value to the existing session array, This will set a new value for name key of a user array.
$request->session()->push('user.name', 'Taylor');
Â
Delete Laravel Session Data
To delete/remove a specific session key on Laravel just use the following method.
$request->session()->forget('key');
Â
Also if you think to retrieve and remove the session, then use the following method.
$value = $request->session()->pull('key', 'default');
Â
To delete multiple sessions. Just use the example below.
$request->session()->forget(['key1', 'key2']);
Â
To delete all session data. Just use the example below.
$request->session()->flush();
Â
We can manually regenerate session ID also just run the following method below. This method will prevent users to make malicious from exploiting a session fixation attack.
$request->session()->regenerate();
Â
To regenerate the session ID and remove all data from the session. Just use the example below.
$request->session()->invalidate();
Â
Now after reading this post you have a basic understanding of the Laravel session I hope this is helpful to you :)
Read next