首页 > 解决方案 > 使用 x-www-form-urlencoded 时 PHP 未填充 $_POST

问题描述

PHP 5.4.17

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

索引.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 变量,所以我觉得问题出在“x-www-form-urlencoded”的默认 enctype 上,但我可以'不知道如何让事情与默认值一起工作。

标签: phpformscontent-type

解决方案


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

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

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

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

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


推荐阅读