phplaravelsession

Session expire time for each session


Can we set expire time for each session in Laravel?

If we can, how is it possible from the controller for each session that we create?


Solution

  • You can change the session lifetime, application wide by changing the lifetime value on config/session.php:

    /*
    |--------------------------------------------------------------------------
    | Session Lifetime
    |--------------------------------------------------------------------------
    |
    | Here you may specify the number of minutes that you wish the session
    | to be allowed to remain idle before it expires. If you want them
    | to immediately expire on the browser closing, set that option.
    |
    */
    
    'lifetime' => 4320,
    'expire_on_close' => false,
    

    Now, if you want to control the session lifetime per user, you need to set this value before logging in the user.

    1. Try if user exists in database
    2. If yes, and he is user who needs longer session lifetime, run config(['session.lifetime' => $newLifetime]);
    3. Log user in
    4. Enjoy longer session lifetime for current user

    ā€” Source

    You have to make the above changes in LoginController.