Laravel:在注册表格打开时,会话期满 [英] Laravel: Session expires while registration form is open

查看:48
本文介绍了Laravel:在注册表格打开时,会话期满的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,正在以信息亭模式在笔记本电脑上等待用户.有时,注册失败,并且用户得到错误屏幕-我认为是419会话已过期.

I have have a web app which is waiting for users on a laptop in kiosk mode. Sometimes, registration fails and users get an error screen - I think it's 419 Session Expired.

因此,我假设登录屏幕加载后两个小时,会话到期(我在config/session.php中保留了默认的120分钟),并且Laravel不接受该页面的任何请求.

So I assume two hours after the login screen loads, the session expires (I kept the default of 120 minutes in config/session.php) and Laravel does not accept any request from that page.

我应该如何处理?我知道如何使用JS每110分钟左右重新加载页面,但是此时我必须检查是否填写了注册表.这对我来说似乎不是一个干净的解决方案.

How should I deal with this? I know how to make a page reload every 110 minutes or so using JS, but then I'd have to check the registration form is being filled out at this moment. This does not feel like a clean solution to me.

是否有其他选择,例如使请求来自注册页面或登录页面的Laravel不太严格的机制?

Is there any alternative, such as a mechanism to make Laravel less strict when a request comes from the register or login pages?

推荐答案

如上所述,最简单的解决方案通常是将会话过期时间从默认的2小时(非常短)延长.

As mentioned, the simplest solution is usually to extend the session expiration time from the default of 2 hours (which is very short).

如果不希望使用更长的会话,则另一种选择是只要使用javascript打开浏览器页面,就可以保持会话存活.

If longer sessions are not desirable, another option is to keep the session alive for as long as the browser page is open by using javascript.

routes/web.php 中添加路由:

Route::post('/keep-alive', function () {
    return response()->json(['ok' => true]);
});

然后使用javascript定期ping此路由:

And then ping this route periodically with javascript:

setInterval(() => {
    axios.post('/keep-alive')
        .then(() => {})
        .catch(() => {})
}, 600000)

(我使用axios发出POST请求,因为它包含在默认的Laravel安装中,但是您可以使用任何方法发出请求.)

(I used axios to make the POST request because it's included with a default Laravel install, but you can use anything to make the request.)

由于请求通过了web中间件组,因此应运行会话中间件并使会话保持活动状态.如果关闭浏览器页面,使计算机进入睡眠状态等,则在经过配置的时间之后,会话仍将正常终止.

Since the request passes through the web middleware group, the session middleware should be run and keep the session alive. If the browser page is closed, the computer is put to sleep, etc., then the session will still expire normally after the configured time has elapsed.

您还可以检查来自javascript调用的会话到期响应,然后刷新页面,提示输入凭据或在检测到会话到期时执行其他操作.如果计算机从睡眠状态恢复运行,则最有可能发生这种情况.

You can also check for session expiration responses from the javascript call and then refresh the page, prompt for credentials, or perform some other action if you detect that the session expired. This case is most likely to occur if the computer resumes operation from a sleeping state.

这篇关于Laravel:在注册表格打开时,会话期满的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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