且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

每个会话的 Laravel 会话过期时间

更新时间:2023-11-28 08:05:40

您可以通过更改 config/session.php上的 lifetime 值来更改应用程序范围内的会话生命周期>:

/*|--------------------------------------------------------------------------|会话生存期|--------------------------------------------------------------------------||您可以在此处指定您希望会话的分钟数|允许在它到期之前保持空闲.如果你想要他们|要在浏览器关闭时立即过期,请设置该选项.|*/'终生' =>4320,'expire_on_close' =>错误的,

现在,如果你想控制每个用户的会话生存期,你需要在登录用户之前设置这个值.

  1. 尝试数据库中是否存在用户
  2. 如果是,并且他是需要更长会话寿命的用户,请运行 config(['session.lifetime' => $newLifetime]);
  3. 登录用户
  4. 为当前用户享受更长的会话生命周期

来源

您必须在 LoginController 中进行上述更改.

Can we set expire time for each session in Laravel?

If we can how it possible from the controller by each session that we create? Thanks.

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.