How does an HTTP session exactly work and in what cases does it expire (in Laravel)?
As you can see, session
is dependent on the driver
you choose, and at the same time you can select the timeout as well in config\session.php
.
In case of Cookie
, the session will expire in two cases:
- Once the cookie has expired/deleted.
- Or
(current_time - cookie_creation_time) > session_timeout
set in thesession.php
.
When the user navigates from the browser and the cookie is still there and it hasn't expired, the user will be identified and session will remain the same.
I hope it's clearer... If not, let me know. I will share some examples.
Laravel session expire time for each session
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.
You have to make the above changes in
- Try if user exists in database
- If yes, and he is user who needs longer session lifetime, run
config(['session.lifetime' => $newLifetime]);
- Log user in
- Enjoy longer session lifetime for current user
— Source
LoginController
.
Related Topics
How to Select a MySQL Database to Use with Pdo in PHP
How to Insert JSON Array into MySQL Database
Get the Get Variables from a Url String
Doctrine2 - "Class" Is Not a Valid Entity or Mapped Super Class
Sending JavaScript Object to PHP via Ajax
PHP MySQL Charset Utf8 Problems
Set Maximum Execution Time for Exec() Specifically
Imagemagick Brew Installation with PHP Module in MAC Os X
File_Get_Contents('Php://Input') Always Returns an Empty String
Doctrine2 Findby Relationship Object Triggers String Conversion Error
How to Automatically Read in Calculated Values with PHPexcel
Laravel Catch Eloquent "Unique" Field Error
PHP MySQL Query Where X = $Variable