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

lyonlim's avatar

Session flash not clearing after redirect

Hi,

When I use Session::flash() or redirect()->with(), the session attribute is stored indefinitely and doesn't get flushed after one redirect.

The only way to clear is it to clear it directly from the database. This is on Laravel 5.2, using database driver (although it's the same behavior with file driver)

I tried this sample code from this thread: https://laracasts.com/discuss/channels/laravel/sessions-are-working-fine-but-flash-data-is-not-working-at-all-help and when I refresh the page at session/result, the flash data persists too.

Can anyone help provide some direction on debugging the issue?

Thanks! Lyon

0 likes
6 replies
Snapey's avatar

If you refresh the same page then you get the same flash message. Do you see it again if you click on a link (eg a nav link)

lyonlim's avatar

@Snapey Yes..in fact, if I store a session attribute status and display it on different pages, the same message appears.

@tisuchi Yes - what specific code?

This is my App/Http/Kernel.php:

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * 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,
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Torann\Currency\Middleware\CurrencyMiddleware::class,
    ];

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

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

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'csrf' => \App\Http\Middleware\VerifyCsrfToken::class,
        'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
        'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
        'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
    ];
}

This is the sample routes I'm using from my first post's link:

Route::get('session/test-flash', function() {
    Session::flash('flash_message', 'This is my flash message!');

    return redirect('session/result');
});

Route::get('session/result', function() {

    if(Session::has('flash_message'))
        echo 'Session "flash_message" variable is: ' . Session::get('flash_message');
    else
        echo 'No flash_message found';
});

Also.. when I run php artisan route:list, both the above routes do not have a web or any other Middleware.

Thank you!

lyonlim's avatar

In addition, if I wrap the two routes within the web Middleware, the flash message does not appear any more.

lyonlim's avatar

Turns out it was due to one of the packages that had a middleware. It would call ->reflash() causing this to happen.

Thanks for your help! I also fixed the weird web middleware by updating the Http\Kernel code from a fresh installation.

Please or to participate in this conversation.