Laravel 5 - 永远保持登录状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42010680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:15:11  来源:igfitidea点击:

Laravel 5 - stay logged in forever

laravellaravel-5

提问by dani24

I wanted to put in the loginpage a checkbox "stay logged in".

我想在login页面中添加一个复选框“保持登录状态”。

That should never end the session of the user unless the user logs out.

除非用户注销,否则永远不会结束用户的会话。

I know that in app/config/session.php I have these variables:

我知道在 app/config/session.php 我有这些变量:

'lifetime' => 60,

'expire_on_close' => false,

But I wanted to maintain alive the session forever, and not be removed if the user close his navigator.

但是我想永远保持会话的活动状态,并且如果用户关闭他的导航器不会被删除。

Any ideas on how should I proceed? Thanks

关于我应该如何进行的任何想法?谢谢

采纳答案by passioncoder

Laravel has a built-in feature to remember users, I'd strongly recommend using it.

Laravel 有一个内置功能来记住用户,我强烈推荐使用它。

If you would like to provide "remember me" functionality in your application, you may pass a boolean value as the second argument to the attempt method, which will keep the user authenticated indefinitely, or until they manually logout. Of course, your users table must include the string remember_token column, which will be used to store the "remember me" token.

如果您想在您的应用程序中提供“记住我”功能,您可以将一个布尔值作为第二个参数传递给尝试方法,这将使用户无限期地进行身份验证,或者直到他们手动注销。当然,您的用户表必须包含字符串 remember_token 列,该列将用于存储“记住我”令牌。

if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
    // The user is being remembered...
}

if you want to solve the problem manually, there is workaround for this: you have to set the expires date of your cookie to a date in the wide future, somewhat like 2050. this is technically not forever, but should usually be long enough.

如果您想手动解决该问题,有一个解决方法:您必须将 cookie 的到期日期设置为未来的某个日期,有点像 2050 年。这在技术上不是永远的,但通常应该足够长。

回答by Vikash

You can set some big values. I have tried now at 7900 years

您可以设置一些大值。我现在已经尝试了 7900 年

i.e, 365*24*60*7990 = 4152240000 minutes

即,365*24*60*7990 = 4152240000 分钟

  'lifetime' => 4152240000

回答by Alexey Mezenin

You can set lifetimeoption to 35791394. It's a maximum possible number for 32-bit machines:

您可以将lifetime选项设置为35791394. 这是32 位机器最大可能数量

'lifetime' => 35791394, // Session will expire in 68 years.