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

SaeedPrez's avatar

[Guide 5.2.x] Make session work in error pages

Hello all,

I'm sure most of you have ran into the problem of some error pages not having your session data which results in unwanted behaviors like when you're logged in and it will show you as logged out.. No worries, the solution is very simple..

Step 1

Locate and open your Kernel.php file which is located in your applications app/Http/ folder.

Step 2

Move the StartSession & EncryptCookies middlewares from the web middleware group into the global HTTP middlware stack.

    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

        // Move the StartSession & EncryptCookies middlewares
        // from the web middleware group to here..
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Session\Middleware\StartSession::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            // \App\Http\Middleware\EncryptCookies::class, <-- Move this line
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            // \Illuminate\Session\Middleware\StartSession::class, <<--- Move this line
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

That's it

All you need to move is two line and of course you can move other middlewares if your application requires it. Your session should now work everywhere in your application.

Hope you find this small guide useful.

0 likes
2 replies
murph133's avatar

Thank you, great help.

I also had to move \Illuminate\View\Middleware\ShareErrorsFromSession::class,

Please or to participate in this conversation.