1

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.

brombeer
  • 6,294
  • 4
  • 18
  • 26
Kabergrammer
  • 9
  • 1
  • 4

1 Answers1

0

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.

Kiran Maniya
  • 5,637
  • 4
  • 34
  • 53
  • 1
    That's obviously not a permanent solution, since you can't ask your users to run with dev tools open… – deceze Feb 14 '20 at 08:22
  • Is there any way to prevent the browser cached page from showing ? and also when I press back doesn't it call my controller again ? I am just used to asp.net mvc where if you press the back button it calls the controller that you've used again and validates the session. – Kabergrammer Feb 14 '20 at 08:22
  • It's not a permanent solution, but we have to disable that if we are developing that. Otherwise, there is no workaround for this. The browser will cache the page every time. – Kiran Maniya Feb 14 '20 at 08:24
  • @Kabergrammer see the updated answer, to disable the view cache. post a comment if the issue still persists. – Kiran Maniya Feb 14 '20 at 08:27
  • 1
    Disabling the *internal* cache won't help. You need to set **appropriate HTTP headers** to prevent the browser from caching pages. – deceze Feb 14 '20 at 08:30
  • @KiranManiya thanks for the answers but i think the solutions not for permanent usage and the disabling of view cache might affect other functions that I have . – Kabergrammer Feb 14 '20 at 08:30
  • @deceze can you share on how to appropriately set HTTP headers ? – Kabergrammer Feb 14 '20 at 08:31
  • https://stackoverflow.com/questions/49547/how-do-we-control-web-page-caching-across-all-browsers – deceze Feb 14 '20 at 08:32
  • @deceze You are right, I was just updating my answer :-) – Kiran Maniya Feb 14 '20 at 08:35