Return to Snippet

Revision: 14807
at June 12, 2009 09:05 by stancell


Initial Code
class TokenProtection
{
	/**
	 * @param string $name
	 * @return string
	 */
	public function getToken($name)
	{
		if (!isset($_SESSION['tokenSalt']))
		{
			$_SESSION['tokenSalt'] = mt_rand(1, 1000000);
		}

		$token = md5($_SESSION['tokenSalt'] . $name);
		return $token;
	}

	/**
	 * @param string $name
	 * @param string $value
	 * @return bool
	 */
	public function isTokenValid($name, $value)
	{
		$expectedValue = self::getToken($name);
		return $value == $expectedValue; 
	}
}

Initial URL

                                

Initial Description

                                

Initial Title
PHP token protection

Initial Tags
php

Initial Language
PHP