gpt4 book ai didi

Check for Session timeout in Laravel(在Laravel中检查会话超时)

转载 作者:bug小助手 更新时间:2023-10-27 20:27:19 25 4
gpt4 key购买 nike



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?

有什么想法吗?


更多回答
优秀答案推荐

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.

把它放在你的“之前”过滤器中--它就会在每次请求时运行。



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;

如果会话超时,则不再设置名称。然后您可以编写一些代码来响应$sessions. exe == 1;



BTW,
Specifically if you need the session lifetime,

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



Use this:

使用这个:



{{ session('lifetime') }}


When describing this,

在描述这一点时,



Use this:

使用以下命令:



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


In Laravel 8 onwards, the best solution seems to be something like this:

从Laravel 8开始,最好的解决方案似乎是这样的:


if(session()->missing("name") && session()->missing("email")) {
// Replace with your own logic to handle the missing session
throw SessionNotFoundException;
}

...where both name and email are two session keys that should always be present in the session. Checking two keys is more robust and prevents the condition triggering when just one of them may be missing due to a bug.

...其中姓名和电子邮件都是应该始终出现在会话中的两个会话密钥。检查两个键更可靠,并防止在其中一个键由于错误而丢失时触发条件。


更多回答

wow i've never seen ::activity() before. Where is that documented?

哇,我以前从来没有见过::Activity()。哪里有记录呢?

It's not documented - I just went to the session class and looked at all the functions to try and work out an answer to your problem :)

它没有文档-我只是去了Session类,并查看了所有函数,试图找出您问题的答案:)

@mattl Although Taylor (Laravel's creator and lead developer) is looking into this to make it easier to do, this answer is definitely the smartest way to go. You really should accept this answer. [:)].

@mattl虽然Taylor(Laravel的创建者和首席开发人员)正在研究这一点,以使其更容易完成,但这个答案绝对是最聪明的方法。你真的应该接受这个答案。[:)].

@TheShiftExchange Call to undefined method Illuminate\Session\Store::activity() why is that? I use Laravel 4

@TheShiftExchange调用未定义的方法Lighmate\Session\Store::Activity()是为什么?我用的是Laravel 4

@giannischristofakis: The activity method appears to be gone from Laravel. An alternate approach is documented on Laravel.io.

@giannischristofakis:活动方法似乎从Laravel中消失了。在Laravel.io上记录了另一种方法。

this doesn't quite do it as I'm trying to check whether there was an item set but has run out of time, not just whether it's there or not.

这并不能完全做到这一点,因为我试图检查是否有一个项目集,但已经用完了时间,而不仅仅是它是否存在。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com