简体   繁体   English

laravel 5.1检查会话是否过期

[英]laravel 5.1 check if a session is expired

I'm working on a laravel 5.1 application and I want to check if a user session has expired after each request, in order to redirect the user to the login page. 我正在使用laravel 5.1应用程序,我想检查每个请求后用户会话是否已过期,以便将用户重定向到登录页面。 in order to do so I have created a middleware that runs on every request, the handle function looks like this 为了做到这一点,我创建了一个可以在每个请求上运行的中间件,handle函数如下所示

public function handle($request, Closure $next)
{
     if(session_status() === PHP_SESSION_NONE)
     {
          return redirect()->guest('login');
     }
     return $next($request);
}

this does not seem to work correctly, because when I type 'localhost:8000' in google chrome it says 'localhost redirected you too many times', I guess it is because the session have not been started since the user is not logged in, so... is there any better way to do this checking? 这似乎无法正常工作,因为当我在Google Chrome浏览器中输入“ localhost:8000”时,它说“ localhost重定向了您太多次”,我猜这是因为用户未登录,因此会话尚未启动,所以...有没有更好的方法来执行此检查?

You can disable middleware in certain routes. 您可以在某些路由中禁用中间件。 by adding the login route to the excepted_urls array. 通过将登录路由添加到excepted_urls数组。 For example, add the following at the beginning of the class: 例如,在类的开头添加以下内容:

 protected $except_urls = [
        'login'
    ];

or you can disable it in your web.php/routes.php depending of the version of Laravel you're using by employing route grouping 或者您可以通过使用路由分组在web.php / routes.php中根据所使用的Laravel版本禁用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM