我对laravel框架很陌生。我正在开发一个应用程序,当用户不活动5分钟后,它会自动注销。我从这里使用了代码:https://github.com/unicodeveloper/laravel-sessiontimeout/blob/master/src/Middleware/SessionTimeout.php,但它似乎对我不起作用。
这是中间件代码:
<?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;
}
}
这是config/session.php
文件
'lifetime' => env('SESSION_LIFETIME', 5),
'expire_on_close' => true,
和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,
];
还有其他我想念的吗? 我已经做了很多研究,但似乎找不到这个问题的答案。如果有人帮助我解决这个问题,我将非常感谢。
答案 0 :(得分:3)
将驱动程序设置为.env中的数据库
CACHE_DRIVER=database
SESSION_DRIVER=file
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
希望这会有所帮助。