Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel customized session.lifetime at user level

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);

default value is as following:

'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.

like image 321
deej Avatar asked Jul 21 '17 21:07

deej


People also ask

How do I increase my session lifetime in Laravel?

If you want to increase your session life time then we need to change in . env file and it is very easy to change it from configuration file in laravel. laravel provides you session. php file there is we can see 'lifetime' key option for setting time in minutes.

How do I reduce session lifetime in Laravel?

Set Session Lifetime in ENV File Ideally, laravel doesn't allow you to increase session expiration time forever; nevertheless, you may set the session expiration time for several minutes or for the next one year. You can now take the total minutes, append these minutes in the .

How long does Laravel session last?

How long does laravel session last? You need to understand what happened: You've set the lifetime of the sessions to 120 minutes, which means after 120 minutes the session is flushed. The remember_me feature is using cookies.

How long is a php session valid?

When the time duration between the current time and the session starting time will be more than 10 minutes, then the current session of the user will be destroyed. The session_unset() and session_destroy() functions have been used in the script as the previous example to destroy the session.


1 Answers

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 App\Http\Middleware;

class ExtendSession
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, $next)
    {
        $lifetime = 2;
        config(['session.lifetime' => $lifetime]);
        return $next($request);
    }
}

Then in the kernel.php file, add this class prior to StartSession.

\App\Http\Middleware\ExtendSession::class,
\Illuminate\Session\Middleware\StartSession::class,
like image 174
levi Avatar answered Oct 12 '22 01:10

levi