Add two hidden fields like this:
<?php
$time = time();
$_SESSION['time'] = $time;
/*
*This is a Perfect Password from Steve Gibbson's site https://www.grc.com/passwords.htm
*It should make for a great Salt.
*/
$salt = 'b79jsMaEzXMvCO2iWtzU2gT7rBoRmQzlvj5yNVgP4aGOrZ524pT5KoTDJ7vNiIN';
$token = sha1($salt . $time);
?>
<input type="hidden" name="token" value="<?php echo $token; ?>" />
Then when you do the authentication you'll use code like this:
<?php
$salt = 'b79jsMaEzXMvCO2iWtzU2gT7rBoRmQzlvj5yNVgP4aGOrZ524pT5KoTDJ7vNiIN';
$token = sha1($salt . $_SESSION['time']);
if($token != $_POST['token'])
{
die('you stupid scum sucking bandwidth hog!');
}
//Rest of form validation
?>
This is not a perfect system and it could be cracked, however one of 2 things would be needed to crack it:
- Your salt (be sure you keep it
secure)
- ALOT of time, this could take several thousands of years to brute
force so don't worry about this.
EDIT: I updated my answer to account for the obvious mistake I made, pass at least the time variable in the session if not both, the time and the hash. Assuming that your using server-side sessions and not cookie sessions then they cannot be tampered with unless someone has access to the server, in which case your already screwed.