如何在 Laravel 5.1 中实现“记住我”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34651823/
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 12:59:35  来源:igfitidea点击:

How implement 'remember me' in laravel 5.1?

phplaravellaravel-5laravel-5.1session-cookies

提问by Neethu

How to implement rememeber me functionality in laravel 5.1? Can anyone give me an example?

如何在 Laravel 5.1 中实现记住我的功能?谁能给我一个例子?

回答by jedrzej.kurylo

Laravel authentication offers remember mefunctionality out of the box.

Laravel 身份验证提供了开箱即用的记住我的功能。

In order to use it you need to do 2 things:

为了使用它,您需要做两件事:

  • add remember_tokencolumn in your users table - this is where the token will be stored
  • pass trueas a second parameter of Auth::attempt()to enable remember mebehaviour
  • 在用户表中添加remember_token列 - 这是存储令牌的位置
  • true作为Auth::attempt()的第二个参数传递以启用记住我的行为

If you do this, Laravel will generate a token that will be saved in users table and in a cookie. On subsequent requests, even if session cookie is not available, user will be authenticated automatically as long as remember-me cookie is there.

如果这样做,Laravel 将生成一个令牌,该令牌将保存在用户表和 cookie 中。在后续请求中,即使会话 cookie 不可用,只要记住我的 cookie 存在,用户就会自动进行身份验证。

You can find more details and example in the docs: https://laravel.com/docs/5.1/authentication#remembering-users

您可以在文档中找到更多详细信息和示例:https: //laravel.com/docs/5.1/authentication#remembering-users