Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincej's avatar
Level 15

How to Clear cache after logout

I tried using this code. However, it does not work. Even after a logout I can use the back button to see the previous pages prior to logout and I can even still interact with the server. However, my session data ie Name and ID, has been cleared. What am I doing wrong?

Many thanks !

public function logout(Request $request)
    {       Auth::guard('web')->logout();
            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");
            $request->session()->flush();
            $request->session()->regenerate();
            return redirect()->route('loggedOut');
    }

0 likes
5 replies
Nakov's avatar

Using browser back button is normal case, but still being able to interact with the site as if there was a logged in user, that means that you are not guarding your endpoints with the auth middleware.So check your routes file for that, and group the protected endpoints with the auth middleware.

Also why do you need to specify the guard on the Auth facade, isn't it web your default guard or you have changes in your auth.php file?

raphael's avatar

@Snapey There comes a problem with this solution on current macOS Safari. If you close the tab and then restore then (CMD+Z "Undo close tab"), the login form is being shown although the user is logged in. This is because Safari does not send any cookies when restoring the tab, hence Laravel thinks there is no active session.

vincej's avatar
Level 15

@snapey Many Thanks! It's working as required!

@nakov I checked my middleware, and fixed a few which were not set correctly. Yes, I have multiple guards for admins and contractors, and so I do user auth:admin and auth:contractor

I have been rebuilding my authentication system for two different user types, "Admin" and "Contractor". After making these adjustments things are working as I want it. However, I am very concerned that I do not why it works. I have checked SO, watched Jeffrey's tutorial on Authentication and read some pieces in Matt Stauffer's book. None of these resouces give a clear and articulate description of how all the pieces work together.

How does a level 50 person get to where they are withou clear resources? Do you spend sleepless nights studying the API?? For me, the API is in itself an incomprehensable document. All advice is welcome! :o)

Many thanks !

jlrdw's avatar

@vincej don't forget, the API has references to the actual code on github, That is what has helped me, seeing how Taylor does something "behind the scenes".

For Auth you have to go to https://github.com/laravel/ui also, I don't know if it has an API.

Please or to participate in this conversation.