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

Php 按下后退按钮时,Laravel注销失败,php,laravel,Php,Laravel,使用Laravel注销方法从my 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注销方法从my Laravel应用程序注销时:

public function getLogout() 
    {
       Auth::logout();
       return Redirect::to('users/login')->with('message', '<div class="alert alert-success">Your have successfully logged out</div>');
    }
公共函数getLogout()
{
Auth::logout();
return Redirect::to('users/login')->带有('message','Your have successfully logue');
}
我已成功注销,但只要按“后退”按钮,我仍然可以访问我的帐户。你知道我该怎么解决这个问题吗


我是拉雷维尔的新手,所以我不确定我的问题是否有意义。在普通PHP中,手动将会话重置为null一直是我的工作。

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

浏览器上的“后退”按钮将为您获取缓存中的最后一页

如果您必须真正防止这种情况,那么您有两种选择:

  • 禁用缓存(通常是个坏主意)。看看吧
  • 如果此keepalive显示用户未登录,则让JavaScript保持页面中资源的活动状态,并重定向用户

  • 就我个人而言,我只会责怪缓存而忽略它。还有第三种选择:使用HTML5历史API,但这可能有点过头了。

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

    就在
    标记之前,我尝试在我的基本模板中使用一些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>
    
    
    $(文档).ready(函数(){
    var isAuth=“”;
    如果(location.href==='http://local.myapp.in/login/')
    {
    if(isAuth)location.href('/home');
    }
    其他的
    {
    如果(!isAuth)location.href('/login');
    }
    });
    
    在上述代码中,替换
    http://local.myapp.in/login/
    使用您的登录页面URL。因此,每次加载页面时,都会执行此代码。如果用户试图在没有loggedin的情况下访问任何受限页面,那么他将被重定向到登录页面。如果用户登录时试图访问
    登录
    页面,浏览器将重定向到
    主页
    页面


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

    我试过了,它可以工作

    路线:

    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');
    
    });
    

    以下是我在Laravel 5中使用GN中间件解决此问题的方法:

    创建一个如下所示的NoCache中间件:

    通过以下步骤:


    然后在kernel.php中注册这个中间件:

    ,因为我是Laravel的新手。所以在Laravel 5.7中,我用自己的方式解决了这个问题。 使用artisan创建中间件

    php artisan make:middleware RevalidateBackHistory
    
    在RevalidateBackHistory中间件中,我们将头设置为no cache和revalidate

    <?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');
        }
    }
    
    在Web.php中更新路由。就我而言

    Route::group(['middleware' => 'revalidate'], function(){
        Route::get('/', 'HomeController@index');
        Route::get('/home', 'HomeController@index');
        Route::get('/dashboard', 'HomeController@index');
    });
    
    就这些!所以基本上,您只需要为需要用户身份验证的路由调用重新验证中间件

    这是我跟踪的url


    我认为这不是注销失败,而是由于浏览器缓存造成的:/您看到了吗?看到了吗?您仍然可以访问您的帐户,或者仍然可以查看缓存的页面?如果在之后刷新会怎么样。@enigmaticus通常我会编写一个基本视图,并在所有其他视图中扩展该视图。因此,我必须只在我的基本视图中编写此代码。嗯,我尝试了这一点,但它不起作用,所以我实现了其他东西,will post is soonso基本上用于这些路由,您没有缓存。如果您没有在登录页面或仪表板上使用太多图像,那么它看起来是一个很好的解决方案。否则有效载荷会很高,它就像一个符咒,但你能解释一下它到底在做什么吗?这将非常有帮助。
    protected $routeMiddleware = [
        .
        .
        'revalidate' => \App\Http\Middleware\RevalidateBackHistory::class,
        .
        .
    ];
    
    Route::group(['middleware' => 'revalidate'], function(){
        Route::get('/', 'HomeController@index');
        Route::get('/home', 'HomeController@index');
        Route::get('/dashboard', 'HomeController@index');
    });