2awm366

OneTimeURL

Mar 11th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <?php
  2.  
  3. /* Enter the site address here,
  4.  * must include the protocol used and the slash */
  5. define("SITE_ADDR", "https://localhost/");
  6.  
  7. /* This is the key used for the HMAC
  8.  * recommend to use `apg` (http://linux.die.net/man/1/apg)
  9.  * or `openssl rand` to generate storng password */
  10. define("MAC_KEY", "Put something random here");
  11.  
  12. /* This defined how long will the URL expire
  13.  * Unit: Seconds
  14.  * Default: 1 Hour */
  15. define("EXPIRE_TIME", 60*60);
  16.  
  17. function GenerateURL($email) {
  18.   $ttl = time() + EXPIRE_TIME;
  19.   $email_hash = hash("md5", $email);  //can use urlencode to replace hashing
  20.   $hmac = hash_hmac("sha256", $email_hash.$ttl, MAC_KEY);
  21.   $query = "op=activate&id=$email_hash&expires=$ttl&signature=$hmac";
  22.  
  23.   return SITE_ADDR . "register.php?$query";
  24. }
  25.  
  26. function VerifyURL($email_hash, $ttl, $hmac) {
  27.   if($ttl < time()) { //Check is the link expired
  28.     return false;
  29.   } elseif($hmac != hash_hmac("sha256", $email_hash.$ttl, MAC_KEY)) { //Check the link signature
  30.     return false;
  31.   } else {
  32.     return true;
  33.   }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment