php – ZF2阻止多个表单提交

在ZF2表单中是否有任何方法可以防止多个表单提交?我已经使用isValid()函数测试了Captcha和CSRF元素,但它们不会阻止多次提交,尤其是那些使用浏览器刷新按钮的提交.
提前致谢

解决方法:

是的,有一个名为PRG的控制器插件:

POST / REDIRECT / GET PLUGIN

引用官方zf2文档:

When a user sends a POST request (e.g. after submitting a form), their
browser will try to protect them from sending the POST again, breaking
the back button, causing browser warnings and pop-ups, and sometimes
reposting the form. Instead, when receiving a POST, we should store
the data in a session container and redirect the user to a GET
request.

http://framework.zend.com/manual/2.0/en/modules/zend.mvc.plugins.html#the-post-redirect-get-plugin

用我自己的话说进一步扩展;使用此插件时,每次POST提交表单时,POST变量都会存储到SESSION中,并且用户会被重定向到不同的路由或简单的相同路由(刷新).然后可以通过PRG插件访问Form变量,作为模仿原始POST数组的简单数组.这样可以防止多次发布FORM.

用法(来自ZF2文档):

// Pass in the route/url you want to redirect to after the POST
$prg = $this->prg('/user/register', true);

if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
    // returned a response to redirect us
    return $prg;
} elseif ($prg === false) {
    // this wasn't a POST request, but there were no params in the flash messenger
    // probably this is the first time the form was loaded
    return array('form' => $myForm);
}

// $prg is an array containing the POST params from the previous request
$form->setData($prg);

// ... your form processing code here
上一篇:PHP-我设法绕过了服务器端集成上的recaptcha-我在做什么错?


下一篇:每日一题 0207