Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

radhamadhavamhostel's avatar

Session expiring within 2 sec

I am using Laravel 10.

Earlier I encountered error "Session store not set on request."

By adding this \Illuminate\Session\Middleware\StartSession::class, in protected middleware The issue was resolved.

protected $middleware = [
        \Illuminate\Session\Middleware\StartSession::class,
        \App\Http\Middleware\LogUserActivity::class,
    ];

But after adding this Session is expiring within 2 or 3 seconds.

My session.php

'driver' => env('SESSION_DRIVER', 'database'),
    'lifetime' => env('SESSION_LIFETIME', 120),
    'expire_on_close' => false,

.env

SESSION_DRIVER=database
SESSION_LIFETIME=120

Table sessions

Column Type Comment

id	| character varying(255)	
user_id	| uuid NULL	
ip_address | 	character varying(45) NULL	
user_agent	| text NULL	
payload	| text	
last_activity | integer	
0 likes
4 replies
Snapey's avatar

go back to the original problem. why would you need to start session again when it is already started in the web middleware group?

list your routes and check what middleware is being applied to each. Make sure nothing is applied twice

radhamadhavamhostel's avatar

@Snapey I have used middleware auth and a custom one

Route::middleware('auth')->group(function () {
Route::get('dashboard', [HomeController::class, 'index'])->name('dashboard'); 

Route::middleware(['PermissionAndRoleCheck:property_module'])->group(function () {
 Route::get('/property', [PropertyController::class, 'index'])->name('property.list');
    Route::get('/property/create', [PropertyController::class, 'create'])->name('property.create');
    Route::post('/property/store', [PropertyController::class, 'store'])->name('property.store'); 
    Route::get('/property/edit/{id}', [PropertyController::class, 'edit'])->name('property.edit');
    Route::get('/property/show/{id}', [PropertyController::class, 'show'])->name('property.show');
    });
 });

my Custom middleware

class LogUserActivity
{
    public function handle($request, Closure $next)
    {
        
        $response = $next($request);
        $subject = $request->session()->get('subject');
        if (Auth::check()) {
            $user = Auth::user();
            $activityLog = new ActivityLog();
            $activityLog->user_id = $user->id;
            $activityLog->subject = $subject;
            $activityLog->url = Request::fullUrl();
            $activityLog->method = $request->getMethod();
            $activityLog->ip = $request->ip();
            $activityLog->agent = $request->header('User-Agent');
            if($subject){
            $activityLog->save();
        }
        }

        return $response;
    }

I have used this middleware to store activity logs. where I pass $subject as session data to this middleware

$subject = 'viewed institution list'; 
$request->session()->flash('subject', $subject);
Snapey's avatar

@radhamadhavamhostel you didn't fancy following my suggestion then

Just posted your extra middleware which you could comment out in 2 seconds to prove that it is not the source of the problem

Snapey's avatar

But after adding this Session is expiring within 2 or 3 seconds.

How do you know?

Please or to participate in this conversation.