Laravel Prevent Browser Back Button After User Logout
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 10 REST API with Passport Authentication Tutorial
- Laravel 10 Resource Route and Controller Example
- Laravel 10 REST API Authentication using Sanctum Tutorial
- Laravel Route Pass Multiple Parameters Example
- How to Get All Routes in Laravel?
- Laravel Redirect to Route from Controller Example
- How to Create Custom Middleware in Laravel?
- How to Deploy Project with Laravel Vapor?
- Laravel Custom Email Verification System Example
- How to Exclude Route from CSRF Middleware in Laravel?
- How to Check Request is Ajax or Not in Laravel?
- Laravel Get Route Parameters in Middleware Example
- How to Make Custom Middleware in Laravel?
- Laravel XSS Protection Middleware Example