php - Links that expire -
so i'm doing password reset , want set time limit. here's code have
$time = time(); $hash = md5($id . $time . "somerandomsalt"); // check again in activation.php $link = "activation.php?id=" . $id . "&hash=" . $hash . "&time=" . $time; would safe? want make link expire after 1 hour. have code generates token (i think code more secure).
$rand = openssl_random_pseudo_bytes(16); // take 8 random bytes $token = substr(sha1($rand), 0, 49); the issue i'm not sure how make
http://localhost:8888/forgot/activate.php?token=467e65833fc977767d5b000929d1229fa5eaf309 expire after time. ideas?
i think safest way store generation time of hash in database. have 2 columns: token , token_created_at (name doesn't matter). when user clicks link, query database token , compare token_created_at current time. if difference more hour, raise error.
edit: find difference, create datetime object token_created_at, , add 1 hour expiration time. compare current time. if current time less expiration time, means token valid, otherwise has expired. code this:
$token_created_at = new datetime('2014-06-28 03:14:07'); $expires_at = $token_created_at->modify('+1 hour'); $current_time = new datetime(); if ($current_time < $expires_at) { // token valid. } else { // token has expired. }
Comments
Post a Comment