0

I am making my recovery password system and I need PHP to wait 2 hours and then UPDATE a specific row in the database's table, how would I do this?

3
  • do you need to expire the record after 2 hours? Commented Jun 19, 2013 at 14:11
  • I need to delete the row. Commented Jun 19, 2013 at 14:13
  • 1
    before checking for the row in your script, delete all rows over 2 hours. i would avoid the cron which will add an additional point of failure Commented Jun 19, 2013 at 14:15

4 Answers 4

4

Store the start time on the user's row, then have a cron job that runs every 5 minutes or so and queries for ones that need to be updated.

OR - when you need to access the data that should have been updated at 2 hours past, check that time and see if it's more than 2 hours, do the update, then get the data.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but is there any quicker way?
Seriously, the above is the easiest and most robust way of doing it. Have a script that runs once a week and cleans up your database, but in general only check the date when someone tries to access that row. You will save yourself so much server resources.
On high traffic sites you can expect people to try and recover their password all the time, so you can use those recover requests to delete old "expired" rows without having to use cron. The overhead should be minimal and no one will notice it. Simple, yet effective.
0

If you are on a dedicated server, you can lunch a CRON every 10min which delete the entry of the table older than 2 hours. Documentation of Cronjobs on Wikipedia

If you are not on a dedicated server, I think your service provider can give you a good way.

1 Comment

This allows for a maximum of two hours and ten minutes recovery time, instead of two hours
0

You don't make PHP wait. You add an expiry timestamp to the recovery information in the database, then check that timestamp when (if?) the user actual goes through the recovery system, e.g.

SELECT ...
WHERE (expirytimestamp >= NOW() - INTERVAL 2 HOUR)

If the user arrives within the 2 hour limit, this query would return the relevant record and they can proceed. If they're past the 2 hour limit, the recovery option is expired and you divert them to a "get another reset email" path.

If you don't want your DB littered with abandoned recovery attempts, then you can have a standard CRON job to go in and delete the expired records.

Comments

0

Programming is one of those things where simplicity means efficiency. By trying to find ways to delete rows on a timed basis, you're adding another job you need to code, and another point of failure.

The best way to implement a two-hour reset restriction is to add a timestamp row to the table (time() in PHP). Then, when you read in the row from the table in the recovery script,

if ($time < time() - 2 * 60 * 60) {
    // Delete row
} else {
    // Perform recovery

This assumes $time to be the stored timestamp, and that you have functions for the deletion and recovery. By the way, timestamps are in seconds, so the $time < time() - 2 * 60 * 60 checks if the stored timestamp is a value two hours in seconds less than the current timestamp.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.