onlinecode

Laravel – How to prevent browser back button after user logout?

Laravel – How to prevent browser back button after user logout?

In this post we will give you information about Laravel – How to prevent browser back button after user logout?. Hear we will give you detail about Laravel – How to prevent browser back button after user logout?And how to use it also give you demo for it if it is necessary.

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 AppHttpMiddleware;


use Closure;


class PreventBackHistory

{

/**

* Handle an incoming request.

*

* @param IlluminateHttpRequest $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 AppHttp;


use IlluminateFoundationHttpKernel 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' => IlluminateAuthMiddlewareAuthenticate::class,

'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,

'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,

'can' => IlluminateAuthMiddlewareAuthorize::class,

'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,

'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,

'prevent-back-history' => AppHttpMiddlewarePreventBackHistory::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

Also see:Laravel – How to Get Route Parameters in your route middleware?

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…..

Hope this code and post will helped you for implement Laravel – How to prevent browser back button after user logout?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version