目前,我尝试使用Zend 2的CSRF保护。
但是,每次我发送我的表单时,我都会收到以下错误消息:
所提交的表格并非来源于预期的网站。
我已经以这种方式实施了CSRF保护:
1)创建了一个表单类并添加了csrf:
$this->add(array(
'type' => 'Zend\Form\Element\Csrf',
'name' => 'secret',
'options' => array(
'csrf_options' => array(
'timeout' => 600
)
)
));2)在视图文件中回显csrf元素:
echo $this->form()->openTag($forgotPasswordForm);
echo $this->formRow($forgotPasswordForm->get('email'));
echo $this->formRow($forgotPasswordForm->get('secret'));
echo $this->formSubmit($forgotPasswordForm->get('submit'));
echo $this->form()->closeTag($forgotPasswordForm);我发现csrf令牌没有存储在会话中,但为什么呢?
发布于 2013-05-02 07:58:36
我的主计长有这样一句话:
$forgotPasswordForm = new ForgotPassword();
$forgotPasswordForm->prepare();我将$forgotPasswordForm->prepare()移到视图文件中,现在它工作了:-)
谢谢您一直鼓励我!
发布于 2014-03-09 11:01:08
您可以在控制器中实例化表单,或者注入DI,但是必须在视图中执行$ form -> view ()。几乎使用隐藏在视图中的de csrf。
我用这张小本子,这是我为ZF2找到的最好的,它几乎有道理2. http://zf2cheatsheet.com/#controller。
这是我使用的代码。
<?php
// module/Album/src/Album/Form/AlbumForm.php:
namespace Album\Form;
use Zend\Form\Form;
use Zend\Form\Element;
class AlbumForm extends Form
{
public function __construct($name = null)
{
parent::__construct('album');
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
/* not including other elements for short answer*/
**$this->add(new Element\Csrf('security'));**
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
<?php
// module/Album/view/album/album/add.phtml:
$form->setAttribute('action', $this->url('album', array(
'action' => 'add' )));
**$form->prepare();**
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formRow($form->get('artist'));
**echo $this->formHidden($form->get('security'));**
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();发布于 2013-11-04 14:08:00
我只是在表单中使用csrf/元素,在输入验证器中使用csrf/验证器。必须使用与元素相同的名称构造csrf/验证器。
https://stackoverflow.com/questions/16305329
复制相似问题