How to prevent browser back button after user logout in laravel?

Today now in this post i will show you How to prevent browser back button after user logout in laravel? Here I am going to share with you an example of how we can prevent back button after the logout in PHP Laravel framework application. So If we are observe this deeply then we are found this fault. When any user logout after this if any user hitting the back button from the browser then it will goes on the home page or any existing page on that he was before in login. But it should be redirect on the Login page instead of the homepage or any others existing pages.

But we can easily prevent this issue by using the middleware in our laravel 6, laravel 7 and laravel 8 any version of application. So we need to create one middleware and then prevent the back button history. So we need to create a new middleware and then use that on middleware in our route.

So, I am going to do this from the scratch . Now just you have to follow the step and then create a new middleware.

Create New Middleware

At First we need to create a new middleware by using the bellow command. So run the bellow command in our laravel application.

php artisan make:middleware PreventBackHistory
Middleware Configuration

Now we have to configuration this in the middleware file. So at first open the PreventBackHistory.php and put the bellow code on that file.


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('Expires','Sun, 02 Jan 1990 00:00:00 GMT');
Register Middleware

Then we need to register the middleware. So open the Kernel.php and add on our new middleware in $routeMiddleware variable array. so We can see in the bellow file how i am add:


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 totally ready to use the “prevent-back-history” middleware in route file. so we can simply use this as like as the bellow example. I am doing with my Laravel 5.8 so i am added in web.php file.


Route::group(['middleware' => 'prevent-back-history'],function(){
	Route::get('/home', 'HomeController@index');

Read Also: How to implement infinite ajax scroll pagination in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook


About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →