In PHP, example of checking to see if 30 minutes h

2019-03-06 15:33发布

问题:

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) Store the date or timestamp in authenticate or last function call in the global variable 2) Compare the current time with last call to see if 30 minutes have passed.

Thanks

回答1:

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();

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
}