按下后退按钮时,Laravel注销失败

时间:2013-11-26 09:34:48

标签: php laravel

使用Laravel注销方法从我的Laravel应用程序注销时:

public function getLogout() 
    {
       Auth::logout();
       return Redirect::to('users/login')->with('message', '<div class="alert alert-success">Your have successfully logged out</div>');
    }

我已成功退出,但点击后退按钮后,我仍然可以访问我的帐户。关于如何解决这个问题的任何想法?

我是laravel的新手,所以我不确定我的问题是否合理。在普通的PHP中,手动将会话重置为null总是为我完成工作。

5 个答案:

答案 0 :(得分:3)

以下是我在Laravel 5 usign中间件中解决它的方法:

像这样创建一个 NoCache 中间件:

完成此操作:How do I implement before vs. after filters in middleware?

class NoCache {
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->headers->set('Cache-Control','nocache, no-store, max-age=0, must-revalidate'); 
        $response->headers->set('Pragma','no-cache'); 
        $response->headers->set('Expires','Fri, 01 Jan 1990 00:00:00 GMT');
        return $response;
    }
}

然后在kernel.php中注册此中间件:Running middleware on every request

答案 1 :(得分:1)

这实际上并不是你想象的那样。

浏览器上的后退按钮为您提取缓存中的最后一页。

如果你必须真的阻止这种情况,那么你有两个选择:

  1. 禁用缓存(通常是个坏主意)。请参阅How to control web page caching, across all browsers?
  2. 如果此keepalive显示用户未登录,则让页面中的资源保持活动状态并重定向用户。
  3. 就个人而言,我只是责备缓存并忽略它。还有第三种选择:使用HTML5历史API,但这可能超过了顶层。

答案 2 :(得分:1)

我试过这个并且有效。

在路线中:

Route::group(array('before' => 'auth', 'after' => 'no-cache'), function()
{
Route::get('dashboard', array('as' => 'getDashboard', 'uses' => 'DashboardController@getIndex'));

Route::get('logout', array('as' => 'getLogout', 'uses' => 'LoginController@getLogout'));

Route::group(array('prefix' => 'users'), function()
{
    Route::get('users', array('as' => 'getUsers', 'uses' => 'UsersController@getIndex', 'before' => 'hasAccess:users.index'));
});
});

在过滤器中:

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

$response->headers->set('Cache-Control','nocache, no-store, max-age=0, must-revalidate');
$response->headers->set('Pragma','no-cache');
$response->headers->set('Expires','Fri, 01 Jan 1990 00:00:00 GMT');

});

答案 3 :(得分:0)

是。正如@Amelia所写,这个问题是因为浏览器缓存而不是Laravel。使用no-cache发送响应是一种解决方案,但这并不总是好的。如果您实施该解决方案,则可能需要支付额外的托管费。

我尝试在</body>标记之前的基本模板中使用一些javascript代码来解决此问题。

<script type="text/javascript">
    $(document).ready(function() {
        var isAuth = "<?php echo Auth::check(); ?>";

        if (location.href === 'http://local.myapp.in/login/')
        {
            if (isAuth) location.href('/home');
        }
        else
        {
            if (!isAuth) location.href('/login');
        }
    });
</script>

在上面的代码中,将http://local.myapp.in/login/替换为您的登录页面网址。因此,每次加载页面时,都会执行此代码。如果用户尝试访问任何受限制的页面而未登录,则他将被重定向到登录页面。如果用户在登录时尝试访问login页面,则浏览器将重定向到home页面。

因为它是js代码,即使页面是从浏览器缓存加载的,这段代码也会运行。

答案 4 :(得分:0)

由于我是Laravel的新手。因此,在Laravel 5.7中,我以自己的方式解决了该问题。 使用artisan创建中间件。

php artisan make:middleware RevalidateBackHistory

在RevalidateBackHistory中间件中,我们将标头设置为不缓存并重新验证。

<?php
namespace App\Http\Middleware;
use Closure;
class RevalidateBackHistory
{
    /**
    * Handle an incoming request.
    *
    * @param \Illuminate\Http\Request $request
    * @param \Closure $next
    * @return mixed
    */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        return $response->header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')
            ->header('Pragma','no-cache')
            ->header('Expires','Fri, 01 Jan 1990 00:00:00 GMT');
    }
}

在Kernel.php中更新应用程序的路由中间件

protected $routeMiddleware = [
    .
    .
    'revalidate' => \App\Http\Middleware\RevalidateBackHistory::class,
    .
    .
];

在Web.php中更新路由。就我而言。

Route::group(['middleware' => 'revalidate'], function(){
    Route::get('/', 'HomeController@index');
    Route::get('/home', 'HomeController@index');
    Route::get('/dashboard', 'HomeController@index');
});

仅此而已!因此,基本上,您只需要为需要用户身份验证的路由调用重新验证中间件即可。

这是我跟踪的网址

Prevent Browser's Back Button Login After Logout in Laravel 5

https://www.youtube.com/watch?v=wLkA1g2s65U

相关问题