問題描述
我正在其中一個中間件(用于 Laravel Web 應用程序)中覆蓋 session.timeout
值,但它似乎不會影響會話超時.雖然如果我調試它會顯示我已經覆蓋的值.
I am overwriting session.timeout
value in one of the middleware (for Laravel web app) but it doesn't seem to be affecting in terms of timing out a session. Though if I debug it shows value I have overwritten.
Config::set('session.lifetime', 1440);
默認值如下:
'lifetime' => 15,
我正在開發的網站對大多數用戶的會話生命周期非常短,但對于選定的用戶,我想提供更長的會話生命周期.
Website that I am working on has very short session lifetime for most of the users but for selected users I want to provide extended session lifetime.
推薦答案
似乎實現動態 lifetime
值的唯一方法是在會話啟動之前在中間件中設置該值.否則為時已??晚,因為應用程序 SessionHandler 已經使用默認配置值進行了實例化.
It seems the only way to accomplish a dynamic lifetime
value, is by setting the value in middleware, before the session gets initiated. Otherwise its too late, as the application SessionHandler will have already been instantiated using the default config value.
namespace AppHttpMiddleware;
class ExtendSession
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $request
* @param Closure $next
* @return mixed
*/
public function handle($request, $next)
{
$lifetime = 2;
config(['session.lifetime' => $lifetime]);
return $next($request);
}
}
然后在kernel.php文件中,在StartSession
之前添加這個類.
Then in the kernel.php file, add this class prior to StartSession
.
AppHttpMiddlewareExtendSession::class,
IlluminateSessionMiddlewareStartSession::class,
這篇關于Laravel 在用戶級別自定義 session.lifetime的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!