本文介绍了在PHP中,检查30分钟是否已经过去的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有身份验证功能。 30分钟后,用户将自动注销。我想将登录时间(时间戳?)存储在authenticate函数中。然后,每次调用需要身份验证的函数时,我都会更新该时间。如果自上次通话以来已过去30分钟,它将自动重新进行身份验证。我将最后一次访问的时间戳或日期存储在全局变量中。我正在查看代码示例,这些示例显示了一种很好的方法:

I have an authenticate function. The user is automatically logged out after 30 minutes. I'd like to store the time of login (timestamp?) in the authenticate function. I will then update that time each time a function requiring authentication is called. If 30 minutes have passed since the last call, it will automatically reauthenticate. I'll store the last access timestamp or date in a global variable. I'm looking code examples showing a good way to:

1)将日期或时间戳记存储在身份验证或最后一个函数调用的全局变量
中2)将当前时间与上次通话进行比较,看看是否已过去30分钟。

1) Store the date or timestamp in authenticate or last function call in the global variable2) Compare the current time with last call to see if 30 minutes have passed.

谢谢

推荐答案

这里需要做的是将时间戳存储在会话中,以便它可以在两次页面加载之间保持不变。
要将当前时间戳存储在会话中,只需执行以下操作:

What you need to do here is to store the timestamp in the session so that it can persist between pageloads.To store the current timestamp in a session just do the following:

$_SESSION['lastAuthTimestamp'] = time();

然后,当您想查看自上次验证以来是否超过30分钟,您可以简单地执行此操作:

Then when you want to see if its been more then 30min since the last auth you can simply do this:

if((time() - $_SESSION['lastAuthTimestamp']) > 30*60)
{
  //more then 30min has passed
}

这篇关于在PHP中,检查30分钟是否已经过去的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 10:26