Home » Php » php – Laravel session bug?

php – Laravel session bug?

Posted by: admin February 25, 2020 Leave a comment

Questions:

Why is that even though I forget a session already it stills prints its value when I press back button ?
Is this kind of a bug in laravel or am i doing something wrong with my code ?

here is how i forget the session

session()->forget('user_role');
session()->forget('user_id');

but even if i do that it still prints the value in the console when I press the back button.
I’ve tried other methods already , such as the pull method and reassigning new values but the problem still exists.

How to&Answers:

The session()->forget('user_role') is performing well. However, You are still able to view/print because browser has cached that page and instead of making a network call, request just heats the cache.

To identify and disable cached for a while, Open up the dev tools and disable cache while dev tool is open and refresh the page. After that, you’ll no longer be able to see that data stored in the session key. For your reference, The page you are seeing is browser cached one.
This is the temporary solution if you want to check the value being washed or not. However, if you don’t want your user to have this behavior, You can try given solutions.

For the browser-based cache control, you should try setting the Cache-Control, Pragma and Expires header with your page response as given,

$contents = View::make('viewname');
return response($contents)-> withHeaders([
    'Cache-Control' => 'no-cache, no-store, must-revalidate',
    'Pragma' => 'no-cache',
    'Expires' => 0
]);

this will allow you to control the page being cached in browser. Setting the Expires to 0 means the page never gonna be cached. You can refer the Official MDN Docs here.

You should also try disabling the Laravel blade view cache if the problem still persists or other solution doesn’t give satisfactory outcome.