Zend CSRF 哈希代码检查是自动处理的吗?
我已将以下代码添加到我的登录表单中
$csrf = $this->createElement('hash', 'csrf', array('salt' => 'unique'));
,在控制器操作中我
if (!$admin_login_form->isValid($_POST)) {
//Throw Error
}else{
// Redirect to index
}
现在的问题是 Zend 是否自动处理哈希代码检查,或者我们是否必须手动编写一些代码来验证它?
I have added following code to my login form
$csrf = $this->createElement('hash', 'csrf', array('salt' => 'unique'));
And in controller action i have
if (!$admin_login_form->isValid($_POST)) {
//Throw Error
}else{
// Redirect to index
}
Now Question is that does Zend handle hash code checking automatically or do we have to code something manually to verify it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是否定的。您无需执行任何操作来检查哈希值是否有效。
当您创建
Zend_Form_Element_Hash
元素时,它会自动将验证器(使用Zend_Validate_Identical
)添加到您的表单中,并将您的哈希注册到新的命名空间会话中。之后,一旦调用
isValid()
方法,如果页面渲染时会话中存储的 CSRF 与上次请求中发送的 CSRF 不同,则相同验证器将失败并返回错误。编辑:
此外,您可以向元素添加盐,并将根据以下加密生成哈希:
md5(mt_rand(1,1000000) . $this->getSalt() . $this->getName( mt_rand(1,1000000)
。要回答评论中的第二个问题,两个元素不会导致任何会话冲突,因为命名空间用于存储散列由三个标准定义:
例如,您的元素可能存储在以下名称空间下:“Zend_Form_Element_Hash_unique_ hash”,其中“unique”是 salt 值和散列你的元素名称。
The answer is no. You don't have to do anything to check wether the hash is valid or not.
When you create a
Zend_Form_Element_Hash
element, it automatically adds a validator (usingZend_Validate_Identical
) to your form and register your hash into a new namespace session.Afterwards, once you call
isValid()
method, if the CSRF stored in the session when the page was rendered is not the same than the one sent in the last request, the Identical validator will fail and return an error.Edit:
Additionally, you can add a salt to your element and a hash will be generate according to the following encryption:
md5(mt_rand(1,1000000) . $this->getSalt() . $this->getName() . mt_rand(1,1000000)
.To answer your second question in your comment, two elements won't cause any session conflicts since the namespace used to store the hash is define by three criteria:
For example, your element would probably be stored under this namespace: "Zend_Form_Element_Hash_unique_ hash" where "unique" is the salt value and hash you element name.