Laravel 5 - How to prevent browser back button after user logout?

By Hardik Savani | October 21, 2016 | | 19389 Viewer | Category : Laravel


Share this post:



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 Laravel middleware. 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.....



We are Recommending you: