Posted By

stancell on 06/12/09


Tagged

php token


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

huynguye


PHP token protection


 / Published in: PHP
 

  1. class TokenProtection
  2. {
  3. /**
  4. * @param string $name
  5. * @return string
  6. */
  7. public function getToken($name)
  8. {
  9. if (!isset($_SESSION['tokenSalt']))
  10. {
  11. $_SESSION['tokenSalt'] = mt_rand(1, 1000000);
  12. }
  13.  
  14. $token = md5($_SESSION['tokenSalt'] . $name);
  15. return $token;
  16. }
  17.  
  18. /**
  19. * @param string $name
  20. * @param string $value
  21. * @return bool
  22. */
  23. public function isTokenValid($name, $value)
  24. {
  25. $expectedValue = self::getToken($name);
  26. return $value == $expectedValue;
  27. }
  28. }

Report this snippet  

You need to login to post a comment.