Laravel 5.6 会话超时自动注销
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51393472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.6 Auto-logout on Session Timeout
提问by Eau De Khantey
I'm very new to laravel framework. I am working on an app that when the user is inactive for 5 minutes, it's automatically log out. I have use code from here: https://github.com/unicodeveloper/laravel-sessiontimeout/blob/master/src/Middleware/SessionTimeout.phpbut it doesn't seem to work for me.
我对 Laravel 框架很陌生。我正在开发一个应用程序,当用户处于非活动状态 5 分钟时,它会自动注销。我使用了这里的代码:https: //github.com/unicodeveloper/laravel-sessiontimeout/blob/master/src/Middleware/SessionTimeout.php但它似乎对我不起作用。
This is the middleware code:
这是中间件代码:
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
use Illuminate\Session\Store;
class SessionTimeOutMiddleware
{
/**
* Instance of Session Store
* @var session
*/
protected $session;
/**
* Time for user to remain active, set to 300 secs( 5 minutes )
* @var timeout
*/
protected $timeout = 300;
public function __construct(Store $session){
$this->session = $session;
$this->redirectUrl = 'auth/login';
$this->sessionLabel = 'warning';
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(! $this->session->has('lastActivityTime'))
{
$this->session->put('lastActivityTime', time());
}
else if( time() - $this->session->get('lastActivityTime') > $this->getTimeOut())
{
$this->session->forget('lastActivityTime');
Auth::logout();
return redirect($this->getRedirectUrl())->with([ $this->getSessionLabel() => 'You have been inactive for '. $this->timeout/60 .' minutes ago.']);
}
$this->session->put('lastActivityTime',time());
return $next($request);
}
/**
* Get timeout from laravel default's session lifetime, if it's not set/empty, set timeout to 15 minutes
* @return int
*/
private function getTimeOut()
{
return ($this->lifetime) ?: $this->timeout;
}
/**
* Get redirect url from env file
* @return string
*/
private function getRedirectUrl()
{
return (env('SESSION_TIMEOUT_REDIRECTURL')) ?: $this->redirectUrl;
}
/**
* Get Session label from env file
* @return string
*/
private function getSessionLabel()
{
return (env('SESSION_LABEL')) ?: $this->sessionLabel;
}
}
}
and this is the config/session.php
file
这是config/session.php
文件
'lifetime' => env('SESSION_LIFETIME', 5),
'expire_on_close' => true,
and in Kernel.php
并在 Kernel.php
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\SessionTimeOutMiddleware::class,
];
Is there anything else I have missed? I have done so many research and seems to can't find the answer to this question. I would really be thankful if someone help me solve this.
还有什么我错过的吗?我做了很多研究,似乎找不到这个问题的答案。如果有人帮我解决这个问题,我真的很感激。
回答by Adnan Mumtaz
Set your driver to database in .env
将您的驱动程序设置为 .env 中的数据库
CACHE_DRIVER=database
SESSION_DRIVER=file
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
Hope this helps.
希望这可以帮助。