Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JennySwift's avatar

Dynamic session lifetime?

Hi, is it possible to make the Laravel session lifetime dynamic? I want the user to be able to choose how long they can be inactive for until they are automatically logged out.

It seems I can only enter a static value for 'lifetime' in config/session.php.

So I coded the functionality in the frontend, then realized it only logged the user out if they had the app open in the browser when the time was up. :)

Could anyone please give me direction on how to do it please?

0 likes
7 replies
crnkovic's avatar

Config::set('sessions.lifetime', $someValue);

3 likes
atherhashmi's avatar

@crnkovic Thanks, this is helpful.

I am looking for a way to override the session timeout (which I can now do using your hint). Yii has a method onBeginRequest where I used to override session timeout, what is the best place to use Config::set('sessions.lifetime', $someValue); in Laravel 5.6?

I am a Laravel newbie so if you could go into a bit of detail, it'd be really helpful. Thanks.

atherhashmi's avatar

@crnkovic BTW, the key should be session.lifetime, you've got sessions.lifetime with an 's'. :)

3 likes
atherhashmi's avatar

@crnkovic Hey again...

I think I managed to do that myself. I needed to override session.lifetime and some other config before every web request. I wrote a middleware, LoadConfig, added it to app/Http/Kernel in the web middleware group and voila, all my requests have the updated config without doing any extra work.

Thanks for the hint, it helped.

Please or to participate in this conversation.