0

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 Answer 1

8

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.