[英]PHP not populating $_POST when using x-www-form-urlencoded
PHP 5.4.17 PHP 5.4.17
I have a simple html form that looks something like this:我有一个简单的 html 表单,看起来像这样:
<form method="POST" action="/addnewaccount.php">
<input type="text" name="firstname" />
<button type="submit">Submit</button>
</form>
<?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
When this form gets submitted, php will not populate the $_POST or $_REQUEST variables.当这个表单被提交时,php 不会填充 $_POST 或 $_REQUEST 变量。 They are just empty arrays.它们只是空数组。
I have checked the following in my php.ini file:我在我的 php.ini 文件中检查了以下内容:
enable_post_data_reading = On
post_max_size = 10M
variables_order = "GPCS"
request_order = "GP"
If I change the form's enctype to "multipart/form-data", the $_POST and $_REQUEST variables are populated, so I feel that the issue is with the default enctype of "x-www-form-urlencoded", but I can't figure out how to get things to work with the default.如果我将表单的 enctype 更改为“multipart/form-data”,则会填充 $_POST 和 $_REQUEST 变量,所以我觉得问题在于“x-www-form-urlencoded”的默认 enctype,但我可以不知道如何让事情在默认情况下工作。
I was able to solve this problem.我能够解决这个问题。 I discovered that it was an interaction of our Node proxy and php.我发现这是我们的 Node 代理和 php 的交互。
In our Node code, we were using the 'body-parser' npm package.在我们的 Node 代码中,我们使用了“body-parser”npm 包。 We had a line where we were using the middleware for form-data like this:我们有一行使用中间件来处理表单数据,如下所示:
app.use(bodyParser.urlencoded({extended: true}));
That middleware converted the form data to JSON before the request was proxied which prevented PHP from getting the data.该中间件在请求被代理之前将表单数据转换为 JSON,这阻止了 PHP 获取数据。
Hopefully this helps someone who might have a similar situation in the future.希望这可以帮助将来可能遇到类似情况的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.