Laravel具有两个不同的生命周期会话 [英] Laravel with two different lifetime sessions

查看:52
本文介绍了Laravel具有两个不同的生命周期会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用laravel框架实现记住我"登录.当用户选中记住我"复选框时,laravel会将会话数据保留一个月.如果用户未选中它,则我希望保持会话状态,直到用户关闭浏览器.

I'm trying to implement 'remember me' login using laravel framework. When user checks 'remember me' box, laravel keeps session data for a month. If user unchecked it, I want to keep session until user closes the browser.

选中记住我"后,我知道如何控制会话.我在config/session.php中配置了会话,如下所示:

I know how to control session when 'remember me' is checked. I configured sessions in config/session.php like below:

'lifetime' => 43200,
'expire_on_close' => false,

我正在使用具有全局会话帮助器的会话

And I'm using sessions with global session helper

session(['accessToken' => SOME_ACCESS_TOKEN]);
Session::flush();

以此类推.

但是当用户以未选中记住我"的身份登录时,我不知道如何工作.我试图找到一种控制配置的方法

But I don't know how to work when user logging in with 'remember me' unchecked. I tried to find a way to control the configuration

'expire_on_close' => true,

根据用户输入即时运行,但失败.请告诉我如何在一个laravel框架中保留两种不同的会话生存期配置.或者,如果我尝试以错误的方式记住我"功能,请指导我.

on the fly, based on user input, but failed. Please tell me how I could keep two different session lifetime configuration within one laravel framework. Or if I'm trying 'remember me' feature in wrong way, please guide me.

基本上,如上所述,我想在一个月内将会话保持为记住我",并且只将会话保持到浏览器关闭而没有记住我".

Basically, like I stated above, I'd like to keep session in a month with 'remember me', and keep session only until browser closing without 'remember me'.

谢谢.

推荐答案

为了获得记住我功能,您不应使用会话配置.相反,您应该使用Laravel已内置的功能.

In order to get remember me functionality you shouldn't work with session configuration. Instead, you should use the functionality already built into Laravel.

当您使用 Auth :: attempt()对用户进行身份验证时,您可以传递布尔值作为第二个参数.如果用户选中了记住我复选框,则将其设置为true.否则将其设置为false.

When you authenticate user with Auth::attempt(), you can pass a boolean value as the second argument. Set it to true, if user checked remember me checkbox. Set it to false otherwise.

此方式,当复选框被选中,用户将被记住,直到他们手动登录应用程序的进行.否则,它们将被记住直到关闭浏览器或会话终止.

This way, when checkbox is checked, user will be remembered till they manually log out of application. Otherwise, they'll be remembered till they close the browser or session expires.

您可以在此处找到更多信息: https://laravel.com/docs/5.3/authentication#remembering-users

You can find some more informaton here: https://laravel.com/docs/5.3/authentication#remembering-users

这篇关于Laravel具有两个不同的生命周期会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆