dr897777 2016-01-23 09:55
浏览 37
已采纳

Zend框架1.12 - 哈希表格验证返回错误

I am using Zend_Form_Element_Hash to protect csrf on my website. Below is the code in my Form.php

$token = new Zend_Form_Element_Hash('token', 'csrf');
$token->setSalt(md5(uniqid(rand(), TRUE)));
$token->initCsrfToken();
$token->initCsrfValidator();
$this->addElement($token);

When I check for $form->isValid in my controller I always get the below error

The two given tokens do not match

Would appreciate any help here!

  • 写回答

1条回答 默认 最新

  • duanqian8867 2016-01-24 21:34
    关注
    1. Csrf is stored in session, session name for csrf depends on salt. If salt is random, session name will be random too. So you couldn't get csrf from session to compare it with csrf from post. Make salt constant for this form.

    2. Don't call initCsrfToken, this method resets csrf and is called automatically on render.

    3. Better transmit salt on element creation

      $token = new Zend_Form_Element_Hash('token', 'csrf', array('salt' => 'secure'));
      $this->addElement($token);
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况