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

elliscristoph's avatar

Logout Destroys sessions

I'm using Laravel 5.5 and I have created a blog-type website with it.

However, I'm having a problem whenever I click the logout button and return back (or press the back button), I am still logged in until I refresh the browser.

Is there any way to disable or destroy all sessions like PHP sessions?

0 likes
10 replies
salahAlkhwlani's avatar

You already destroys session, but browser serve cached page when press back button

so you need to force the browser to prevent cache page, you can use this middleware

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

class NoHttpCache
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // This step is only needed if you are returning
        // a view in your Controller or elsewhere, because
        // when returning a view `$next($request)` returns
        // a View object, not a Response object, so we need
        // to wrap the View back in a Response.
        if (!$response instanceof SymfonyResponse) {
            $response = new Response($response);
        }

        /**
         * @var  $headers  \Symfony\Component\HttpFoundation\HeaderBag
         */
        $response->header('Pragma', 'no-cache');
        $response->header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
        $response->header('Cache-Control', 'no-cache, must-revalidate, no-store, max-age=0, private');

        return $response;
    }
}

then in app/Http/Kernel.php add middleware as global for all requests

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            ......
            NoHttpCache::class,
            ......
        ],
    ];
Snapey's avatar

You just need to redirect to a different page after logout.

1 like
salahAlkhwlani's avatar

@elliscristoph the namespace of middleware class is incorrect,

make sure the class in this path app/Http/Middleware/NoHttpCache.php and add \App\Http\Middleware\NoHttpCache::class to kernel file.

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            ......
            \App\Http\Middleware\NoHttpCache::class,
            ......
        ],
    ];
Snapey's avatar

Why would you want your users to not cache your website? You realise this is for every page?

elliscristoph's avatar

@Snapey The problem here is that when a user logs out of their accounts and presses the back button the site shows the previous webpage while they are still logged in which can be a fail in security purposes. Unlike in PHP sessions where I can session_start and session_destroy so it'll auto re-direct to login.php even if they press the back button after they logged out.

Snapey's avatar

logout does session destroy (actually it creates a new session)

1 like

Please or to participate in this conversation.