防止表单的重复提交功能

1、在模板页面中添加一个隐藏的表单
<input type=”hidden” value=”{$formash}” id=”formhash” />
2、通过控制器显示页面的时给$formash传递一个值,并记录到setcookie。
$formash = md5(time()+rand(0,1000));
@setcookie(‘formash’,$formash,0,’/‘);
$smarty->set(‘formash’,$formash); //通过smarty模板或其它类型的模板将值传递熬模板文件中
3、提交表单后,验证formhash与cookie中的值是否相等,如果相等的情况下,将secookie设为过期,不相等则表示表单已经重复提交,返回用户一个提示(请勿重复提交)
写一个小函数
$formash = $_POST[‘formash’];
function checkformhash($hash = ‘’)
{
if(!$hash)
{
return false;
}
if($hash != $_COOKIE[‘formash’])
{
return false;
}
setcookie(‘formash’,$hash,time() - 86400,’/‘);
return true;
}

通过如此方式即可防止用户重复提交表单。