【问题标题】:PHP not populating $_POST when using x-www-form-urlencoded使用 x-www-form-urlencoded 时 PHP 未填充 $_POST
【发布时间】:2020-04-26 13:27:32
【问题描述】:

PHP 5.4.17

我有一个简单的 html 表单,看起来像这样:

index.html

<form method="POST" action="/addnewaccount.php">
    <input type="text" name="firstname" />
    <button type="submit">Submit</button>
</form>

addnewaccount.php

<?php
var_dump($_POST); // array(0) {}
var_dump($_REQUEST); // array(0) {}
var_dump(file_get_contents('php://input')); //string(0) ""
var_dump($HTTP_RAW_POST_DATA); // NULL

当这个表单被提交时,php 不会填充 $_POST 或 $_REQUEST 变量。它们只是空数组。

我在我的 php.ini 文件中检查了以下内容:

enable_post_data_reading = On
post_max_size = 10M
variables_order = "GPCS"
request_order = "GP"

如果我将表单的 enctype 更改为“multipart/form-data”,则会填充 $_POST 和 $_REQUEST 变量,所以我觉得问题在于默认的 enctype 为“x-www-form-urlencoded”,但我不知道如何让事情使用默认值。

【问题讨论】:

  • var_dump(file_get_contents("php://input")); 是否输出任何内容?如果没有,那么大多数我会检查您的网络服务器中是否没有特殊配置来限制这一点。另外:您是否安装了suhosin?通过打印您的phpinfo(); 来检查这一点(比检查php.ini 更好,以防您错过了在某处加载其他模块)。
  • 你检查过这个问题的答案了吗:php $_POST array empty upon form submission你的情况可能就是其中之一。
  • 谢谢@BartoszPachołek。因为我没有发布 JSON,所以 php://input 只是一个空字符串。
  • 谢谢@Accountantم,我已经查看了其他 StackOverflow 问题中的答案,但都没有解决问题。

标签: php forms content-type


【解决方案1】:

我能够解决这个问题。我发现这是我们的 Node 代理和 php 的交互。

在我们的 Node 代码中,我们使用了“body-parser”npm 包。我们有一行使用中间件处理表单数据,如下所示:

app.use(bodyParser.urlencoded({extended: true}));

该中间件在请求被代理之前将表单数据转换为 JSON,从而阻止 PHP 获取数据。

希望这对将来可能遇到类似情况的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-03
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 2019-02-04
    • 1970-01-01
    • 2018-11-26
    相关资源
    最近更新 更多