ZF2阻止多表单提交

myn*_*ame 3 php captcha zend-framework2

在ZF2表单中是否有任何方法可以防止多个表单提交?我已经测试Captcha,并CSRF与元素isValid()的功能,但他们不`吨防止多次提交特别是那些有浏览器的刷新按钮.提前致谢

mat*_*twr 5

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

POST/REDIRECT/GET PLUGIN

引用官方zf2文档:

当用户发送POST请求时(例如,在提交表单之后),他们的浏览器将尝试保护他们不再发送POST,打破后退按钮,导致浏览器警告和弹出窗口,有时还会重新发布表单.相反,当接收POST时,我们应该将数据存储在会话容器中并将用户重定向到GET请求.

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
Run Code Online (Sandbox Code Playgroud)