By Kabergrammer


2020-02-14 08:14:17 8 Comments

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.

1 comments

@Kiran Maniya 2020-02-14 08:19:25

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.

@deceze 2020-02-14 08:22:42

That's obviously not a permanent solution, since you can't ask your users to run with dev tools open…

@Kabergrammer 2020-02-14 08:22:58

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.

@Kiran Maniya 2020-02-14 08:24:11

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 2020-02-14 08:27:39

@Kabergrammer see the updated answer, to disable the view cache. post a comment if the issue still persists.

@deceze 2020-02-14 08:30:02

Disabling the internal cache won't help. You need to set appropriate HTTP headers to prevent the browser from caching pages.

@Kabergrammer 2020-02-14 08:30:32

@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 2020-02-14 08:31:19

@deceze can you share on how to appropriately set HTTP headers ?

@deceze 2020-02-14 08:32:20

@Kiran Maniya 2020-02-14 08:35:29

@deceze You are right, I was just updating my answer :-)

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] How do I expire a PHP session after 30 minutes?

  • 2009-02-06 13:14:14
  • Tom
  • 947634 View
  • 1032 Score
  • 14 Answer
  • Tags:   php session cookies

1 Answered Questions

Laravel - preventing the user from clicking back button

  • 2020-01-19 00:47:36
  • H.Karatsanov
  • 36 View
  • 0 Score
  • 1 Answer
  • Tags:   php laravel

4 Answered Questions

[SOLVED] Laravel Unknown Column 'updated_at'

20 Answered Questions

[SOLVED] Best Practices for Custom Helpers in Laravel 5

22 Answered Questions

[SOLVED] Laravel requires the Mcrypt PHP extension

2 Answered Questions

Laravel session not destorying

1 Answered Questions

[SOLVED] Working with Laravel 5.3 cookies

  • 2017-03-07 15:42:41
  • Aarón Gutiérrez
  • 516 View
  • 1 Score
  • 1 Answer
  • Tags:   php laravel cookies

2 Answered Questions

[SOLVED] Can't forget session from ajax request in laravel 5.2

Sponsored Content