gpt4 book ai didi

php - ZF2 防止提交多个表单

转载 作者:可可西里 更新时间:2023-11-01 00:43:39 26 4
gpt4 key购买 nike

ZF2 表单中有什么方法可以防止多次提交表单吗?我已经使用 isValid() 函数测试了 CaptchaCSRF 元素,但它们不会阻止多次提交,尤其是那些带有浏览器刷新按钮的元素。提前致谢

最佳答案

是的,有一个名为 PRG 的 Controller 插件:

发布/重定向/获取插件

引用官方 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 插件访问表单变量,作为模仿原始 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 - ZF2 防止提交多个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26861561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com