【问题标题】:php://input <> $_POST?php://input <> $_POST?
【发布时间】:2011-06-09 21:18:48
【问题描述】:

我正在试验 Firefox 的内容安全策略。基本上它是一个特殊的网页标头,告诉浏览器哪些资源是有效的。

当某些资源因违反政策而无效时,Firefox 会以 json 格式向给定 URI 发送报告。

这是一份典型的报告

array(1) {
  ["csp-report"]=>
  array(4) {
    ["request"]=>
    string(71) "GET http://example.com/?function=detail&id=565 HTTP/1.1"
    ["request-headers"]=>
    string(494) "Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0b10pre) Gecko/20110115 Firefox/4.0b10pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-ar,en-us;q=0.8,es;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: UTF-8,*
Keep-Alive: 115
Connection: keep-alive
Referer: http://example.com/index.php?function=search&query=Pata+de+cambio+
Cookie: the cookie
"
    ["blocked-uri"]=>
    string(4) "self"
    ["violated-directive"]=>
    string(30) "inline script base restriction"
  }
}

内容类型为application/json; charset=UTF-8

现在。我希望这可以在 $_POST 中作为 REQUEST_METHOD==POST 但 post 总是空的。 我可以从 php://input 访问它,但问题是:为什么请求在 $_POST 中不可用?

我什至不能使用 filter_input 并且 $_REQUEST 是空的...

【问题讨论】:

    标签: php json firefox input


    【解决方案1】:

    $_POST 为您提供表单变量,它们在页面中显示如下:

    POST /some_path HTTP/1.1
    
    myvar=something&secondvar=somethingelse
    

    但是您得到的不是有效的查询字符串。它可能看起来像这样:

    POST /some_path HTTP/1.1
    
    {'this':'is a JSON object','notice':'it\'s not a valid query string'}
    

    php://input 以原始形式为您提供标题之后的所有内容,因此在这种情况下,我认为这是获得所需内容的唯一方法。

    【讨论】:

    • 嗯,我想你说得有道理。但通常 json 数据是在没有问题的情况下发送的。为什么在这种特殊情况下它会出现而不是通过 javascript 发布?
    • @The Disintegrator:我这方面的经验很少,但是我使用的框架(ExtJS)将JSON对象转换为查询字符串。
    • @BrendanLong。你会想在 it's 中转义那个撇号。
    【解决方案2】:

    如果请求以POST 发送,则不一定编码为普通application/x-www-form-urlencodedmultipart/form-data。如果 Firefox 发送一个 JSON 正文,那么 PHP 不知道如何解码它。

    你必须检查$_SERVER["HTTP_CONTENT_TYPE"]。如果它包含application/json,那么您确实必须阅读 php://stdin:

    if (stripos($_SERVER["HTTP_CONTENT_TYPE"], "application/json")===0) {
         $_POST = json_decode(file_get_contents("php://input"));
    // or something like that
    

    【讨论】:

      【解决方案3】:

      它可以是其他几种 http 请求类型(我现在知道 7 种,还有几个占位符,以后还会有更多)。
      我会打印 $_REQUEST$_SERVER 来看看它是如何到达的。

      【讨论】:

      • 没有报告。 $_REQUEST 为空,$_SERVER 有典型数据
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 2018-11-26
      相关资源
      最近更新 更多