Laravel 在用户级别自定义 session.lifetime

Laravel customized session.lifetime at user level(Laravel 在用户级别自定义 session.lifetime)
本文介绍了Laravel 在用户级别自定义 session.lifetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在其中一个中间件(用于 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)