【问题标题】:Adding CSRF to formtoemailpro.php将 CSRF 添加到 formtoemailpro.php
【发布时间】:2018-06-18 08:21:09
【问题描述】:

关注https://codeutopia.net/blog/2008/10/16/how-to-csrf-protect-all-your-forms/的帖子

我正在尝试将代码添加到 formtoemailpro.php https://formtoemail.com/developer_pricing.php

我将密钥生成器添加到表单页面:

//Generate a key, print a form:
$key = sha1(microtime());
$_SESSION['csrf'] = $key;

还有隐藏字段:

<input type="hidden" name="csrf" value="<?php echo $key; ?>" />

但我无法将以下内容添加到 formtoemailpro.php 并使其正常工作。我已将上半部分放在表单的顶部,将“}”放在底部,并添加了“if(!isset”表达式以及表单处理部分的其他类似元素,但我失败了。

if($_SERVER['REQUEST_METHOD'] == 'POST')
            //Here we parse the form
if(!isset($_SESSION['csrf']) || $_SESSION['csrf'] !== $_POST['csrf'])
 throw new RuntimeException('CSRF attack');
 //Do the rest of the processing here
}

我很抱歉我的无知,请任何人帮助我吗?

谢谢。

【问题讨论】:

  • if之前显示$_POST['csrf']的值和$_SESSION['csrf']的值,你就明白问题出在哪里了

标签: php forms csrf


【解决方案1】:

如果我低估了你的问题,这就是你需要做的:

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //Here we parse the form
}

if(!isset($_SESSION['csrf']) || $_SESSION['csrf'] !== $_POST['csrf']){
    throw new RuntimeException('CSRF attack');
 //Do the rest of the processing here
}

确保将“{”、“}”放在代码块上。

另外,确保在任何输出之前调用$_SESSION['csrf'] = $key;。 (回声,打印等......)。看this关于会话的问答。

【讨论】:

  • 谢谢。我的问题是将代码添加到表单处理器。我不能在这里真正发布脚本,因为它是付费的,我不想让它免费提供,但是,它的策略是: 抱歉 - 我想这没什么帮助
  • @user2135841 尝试更改“error_reporting(E_ALL ^ E_NOTICE);”到错误报告(假);正如您将在链接中看到的,您应该添加 session_start();并且所有 $_session[] 调用必须在任何文件输出之前。 (顺便说一句 - 如果一切顺利,请不要忘记投票并选择答案:)
  • 谢谢。恐怕我在技术上无法做到这一点。
  • 很高兴为 @user2135841 提供帮助,欢迎来到 Stack Overflow。如果此答案解决了您的问题,请将其标记为已接受并投票。谢谢,祝你好运!
猜你喜欢
  • 2017-05-30
  • 1970-01-01
  • 2018-10-08
  • 2016-09-19
  • 2016-12-20
  • 2012-11-19
  • 2011-04-28
  • 2018-02-17
  • 2018-07-28
相关资源
最近更新 更多