ItSolutionStuff.com

Laravel Prevent Browser Back Button After User Logout

By Hardik Savani • April 16, 2024
Laravel

Today, I am going to share with you How to prevent back button after logout in PHP Laravel framework. If you observe deeply then you found this fault, When user logout after if user hitting back button from browser then it will goes on home page or existing page that he was before login. But it should redirect on Login page instead of homepage or other existing page.

But we can prevent this issue by using middleware in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11. We will create one middleware and prevent back button history. So we have to create new middleware and use that middleware in our route.

So, I am going to do from scratch so, just you have to follow step and create new middleware.

Create New Middleware

First we will create new middleware using bellow command, so run bellow command in your laravel application.

php artisan make:middleware PreventBackHistory

Middleware Configuration

Now we we have to configuration in middleware file, so first open PreventBackHistory.php and put bellow code on that file.

app/Http/Middleware/PreventBackHistory.php

<?php


namespace App\Http\Middleware;


use Closure;


class PreventBackHistory

{

/**

* Handle an incoming request.

*

* @param \Illuminate\Http\Request $request

* @param \Closure $next

* @return mixed

*/

public function handle($request, Closure $next)

{

$response = $next($request);

return $response->header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')

->header('Pragma','no-cache')

->header('Expires','Sun, 02 Jan 1990 00:00:00 GMT');

}

}

Register Middleware

Now we have to register middleware, so open Kernel.php and add our new middleware in $routeMiddleware variable array, so you can see bellow file how i added:

app/Http/Kernel.php

<?php


namespace App\Http;


use Illuminate\Foundation\Http\Kernel as HttpKernel;


class Kernel extends HttpKernel

{

.....

.....

/**

* The application's route middleware.

*

* These middleware may be assigned to groups or used individually.

*

* @var array

*/

protected $routeMiddleware = [

'auth' => \Illuminate\Auth\Middleware\Authenticate::class,

'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,

'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,

'can' => \Illuminate\Auth\Middleware\Authorize::class,

'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

'prevent-back-history' => \App\Http\Middleware\PreventBackHistory::class,

];

}

Use Middleware In Route

Now we are ready to use "prevent-back-history" middleware in route file, so you can simply use like as bellow example, I am doing with Laravel 5.3 so i added in web.php file.

routes/web.php

Route::group(['middleware' => 'prevent-back-history'],function(){

Auth::routes();

Route::get('/home', 'HomeController@index');

});

You can simply use "prevent-back-history" middleware where you require.

I hope Maybe it can help you.....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 10 REST API with Passport Authentication Tutorial

Read Now →

Laravel 10 Resource Route and Controller Example

Read Now →

Laravel 10 REST API Authentication using Sanctum Tutorial

Read Now →

Laravel Route Pass Multiple Parameters Example

Read Now →

How to Get All Routes in Laravel?

Read Now →

Laravel Redirect to Route from Controller Example

Read Now →

How to Create Custom Middleware in Laravel?

Read Now →

How to Deploy Project with Laravel Vapor?

Read Now →

Laravel Custom Email Verification System Example

Read Now →

How to Exclude Route from CSRF Middleware in Laravel?

Read Now →

How to Check Request is Ajax or Not in Laravel?

Read Now →

Laravel Get Route Parameters in Middleware Example

Read Now →

How to Make Custom Middleware in Laravel?

Read Now →

Laravel XSS Protection Middleware Example

Read Now →