php 检查 Laravel 中的会话超时

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

Check for Session timeout in Laravel

phplaravellaravel-3

提问by mattl

I was just wondering if anyone knew how to check for session timeout in Laravel.

我只是想知道是否有人知道如何在 Laravel 中检查会话超时。

You can check whether the session has a specific item:

您可以检查会话是否具有特定项:

if (Session::has('name'))
{
     $name = Session::get('name');
}

But you can't check whether the session has expired. It would be nice so that I can report back to the user in a more specific way. "Your session has timed out, please start again."

但是您无法检查会话是否已过期。如果我能以更具体的方式向用户报告,那就太好了。“您的会话已超时,请重新开始。”

Any thoughts?

有什么想法吗?

回答by Laurence

Just use the same logic as the session class itself.

只需使用与会话类本身相同的逻辑即可。

if ((time() - Session::activity()) > (Config::get('session.lifetime') * 60))
{
   // Session expired
}

Place this in your 'before' filter - and it will run on every request.

把它放在你的“before”过滤器中——它会在每个请求上运行。

回答by thestepafter

Why not do this?

为什么不这样做?

if (!Session::has('name'))
{
     $sessionTimeout = 1;
}

If a session times out then the name will no longer be set. You can then write some code to respond to $sessionTimeout == 1;

如果会话超时,则将不再设置名称。然后你可以写一些代码来响应 $sessionTimeout == 1;

回答by Sahin S.

BTW, Specifically if you need the session lifetime,

顺便说一句,特别是如果您需要会话生存期,

Use this:

用这个:

{{ session('lifetime') }}

When describing this,

在描述这件事时,

Use this:

用这个:

session(['User' => $user, 'lifetime' => 3600]);