In this tutorial you will learn about the Laravel Clear Cache After Logout and its application with practical example.
Laravel Clear Cache After Logout
In this example, we will learn to destroy session, cookies, cache and prevent browser back button after user logout, so that he will not be able to access auth protected routes or pages after logout.
In Laravel, it is quite a common problem that when user logout and then click on the back button in browser the browser shows the last loaded page from the website. In this article, I’ll show you how to prevent back button after logout in Laravel application. In Laravel, when a user logout from application and then hit the browser back button then he will be sent back to the page he was visiting before logout. Ideally, user should not be allowed to access auth middleware protected routes or pages. User should be redirect back to application login page instead of the page he was visiting.
Here is the logout function code that worked for me:
1 2 3 4 5 6 7 8 9 10 |
public function logout(Request $request) { header("cache-Control: no-store, no-cache, must-revalidate"); header("cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); Session::flush(); $request->session()->regenerate(); Session::flash('succ_message', 'Logged out Successfully'); return redirect('signin'); } |
Here, we are flushing the session and cache then after redirecting user to login page.