Laravel 5 – Prevent Browser back button after Logout

In this post, I would like to share a simple Laravel5 Middleware which prevents going back to the previous protected page after Logout by hitting the back button on your Browser.

When you hit the browser back button after logout, it will take you to the original page instead of redirecting to login page. It should redirect to the login page, isn’t it? Here I am gonna share a Middleware which will fix this issue.

Prevent Browser back button after Logout
Let’s start –

Create middleware

Create a middleware by issuing artisan command from your terminal –

Laravel Prevent Browser back button

Above command will generate a middleware class called ValidateBackHistory in app/http/middleware. Open that file and add response header configurations as shown below.

Now open \app\Http\Kernel.php file, add our custom ValidateBackHistory middleware in route middleware’s array.

To use this middleware you just need to call validateBackHistory middleware for routes which require user authentication.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 vote
Article Rating
Subscribe
Notify of
guest
9 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
srikanth
srikanth
5 years ago

Nice Site & very help for me & coming to my doubt , after inactivity times exceeds , will it automatically redirects to login page or we need to hit particular routes for checking above middle ware condition…

Thanks In advance

arjun
5 years ago
Reply to  srikanth

Thanks Srikanth. Definitely you need to hit route(any) then only middleware will get executed and kills the active session(if inactivity time exceeds). Then if the route is auth protected it will redirect to login page else it will go to requested page.

anakpak amid
anakpak amid
4 years ago

i had use this solution. its very good.but i’m having problem to hide the header message

Pras
Pras
2 years ago

Hello arjun, I’ve been tried this solution. But I still get cache and that’s mean I still get dashboard page again. I use Laravel 5.6. Do u have any other solution?
Sorry I’m a newbie in laravel :).

arjun
2 years ago
Reply to  Pras

It should work, are you calling validateBackHistory middleware on routes(dashboard page) ..

valli vasu
valli vasu
1 year ago

Hi i need some help.this not working

testdeveloper30
testdeveloper30
1 year ago

Is there anyway, to not show login page.
What I am looking for is, If user logged-in and moved to dashboard page, then on dashboard page (clicking on browser) back button should not redirect user to login page as user is already logged in.

testdeveloper30
testdeveloper30
1 year ago

afdasf

testdeveloper30
testdeveloper30
1 year ago

sfafad

DMCA.com Protection Status
9
0
Would love your thoughts, please comment.x
()
x