PHP token protection


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.