Laravel Session

What is a Session?

A Session in Laravel is a way to store user data across multiple requests.

Since HTTP is stateless, sessions help in remembering data like login information, shopping cart items, preferences, etc., until the user closes the browser or logs out.

👉 Example: When a user logs in, their user ID is stored in the session so they stay logged in across different pages.


 Session Configuration

Sessions are configured in config/session.php.

Important options:

  • driver → how session is stored (file, database, cache, cookie).
  • lifetime → how long (in minutes) a session lasts.
  • expire_on_close → whether to expire when browser closes.
  • encrypt → whether to encrypt session data.

Example (.env):

SESSION_DRIVER=file
SESSION_LIFETIME=120

Session Drivers in Laravel

Laravel supports different storage drivers for sessions:

  1. file – default, stores sessions in storage/framework/sessions.
  2. cookie – stores sessions inside encrypted cookies.
  3. database – stores sessions in DB table (sessions).
  4. redis – stores in Redis (fast, scalable).
  5. memcached – for caching systems.
  6. array – stores only for current request (useful for testing).

👉 Use php artisan session:table to create a sessions table for database driver.


Basic Session

web.php (routes\web.php)

Route::get('/session_store', function () {
    session(['user_name' => 'john']);       // Store user name in session
    return "Session Stored";
});
Route::get('/session_get', function () {
    return session('user_name');            // Get user name from session
});
Route::get('/session_clear', function () {
    session()->forget('user_name');         // Remove user name from session
    return "Session Removed";
});


Output Store Session

Output Get Session

Output Remove Session


How to Use Session in Laravel

1. Store Data in Session

session(['user_id' => 1]); // Store user id
session(['username' => 'John']); // Store name

OR

$request->session()->put('user_id', 1);


2. Get Data from Session

$userId = session('user_id');
$username = $request->session()->get('username');


3. Check if Data Exists

if ($request->session()->has('user_id')) {
    // User is logged in
}


4. Remove Data

$request->session()->forget('user_id'); // remove one key
$request->session()->flush(); // remove all session data


5. Regenerate Session ID

To prevent session fixation attacks:

$request->session()->regenerate();

Flash Data (Temporary Session)

Flash data only lasts for the next request (useful for messages after redirect).

Example:

$request->session()->flash('status', 'Profile updated successfully!');

In Blade:

@if (session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
@endif

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.