gpt4 book ai didi

Laravel 5.6 session 超时自动注销

转载 作者:行者123 更新时间:2023-12-02 10:43:45 26 4
gpt4 key购买 nike

我对 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,
];

还有什么我错过的吗?我做了很多研究,似乎找不到这个问题的答案。如果有人帮我解决这个问题,我将非常感激。

最佳答案

将驱动程序设置为 .env 中的数据库

CACHE_DRIVER=database
SESSION_DRIVER=file
SESSION_LIFETIME=120
SESSION_ENCRYPT=false

希望这有帮助。

关于Laravel 5.6 session 超时自动注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com