Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove Cache of a Specific Page

when implementing authentication in laravel using sentry and logging out, then if I press 'go back one page' button of any browser it goes back to the dashboard. If the page is refreshed, it goes to the login page as desired. But I want to prevent accessing dashboard without refreshing.

  1. How to remove the cache of this specific page instantly after logging out?
  2. How to find out any browser's specific cache of a page and Laravel's approach of doing it?

N.B. After logging out and going to the dashboard in this manner prevents from changing anything as needed.

like image 262
Nasif Md. Tanjim Avatar asked Nov 10 '22 23:11

Nasif Md. Tanjim


1 Answers

Destroy the Session when you call the logout function. Simply write your logout function in your controller like this:

public function getLogout() {
        Sentry::logout();
        Session::flush(); // Insert this line, it will  remove  all the session data
        return Redirect::to('users/login')->with('message', 'Your are now logged out!');
    }

Edit:

First I used only Session:flush(), and somehow it worked! But when I checked again, I found that it's not working. So, we need to add some code more to clear the browser cache when log out.

Using filter can be a solution for this problem. (I didn't find any other solution yet) First, add this code in filters.php :

Route::filter('no-cache',function($route, $request, $response){

    $response->header("Cache-Control","no-cache,no-store, must-revalidate");
    $response->header("Pragma", "no-cache"); //HTTP 1.0
    $response->header("Expires"," Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

});

Then attach this filter to routes or controllers. I attached it into construct function of controller like this:

public function __construct() {
        $this->beforeFilter('csrf',array('on' => 'post'));
        $this->afterFilter("no-cache", ["only"=>"getDashboard"]);
    }
like image 51
Tahsin Abrar Avatar answered Nov 14 '22 23:11

Tahsin Abrar