简体   繁体   English

使用 x-www-form-urlencoded 时 PHP 不填充 $_POST

[英]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 表单,看起来像这样:

index.html索引.html

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

addnewaccount.php添加新帐户.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

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.

相关问题 php-使用php处理应用程序/ x-www-form-urlencoded POST请求 - php - Handle application/x-www-form-urlencoded POST request using php 在PHP中使用cURL和x-www-form-urlencoded的POST返回访问被拒绝 - POST using cURL and x-www-form-urlencoded in PHP returning Access Denied Post方法适用于原始格式,但不适用于x-www-form-urlencoded POSTMAN - Post method works in raw but not in x-www-form-urlencoded POSTMAN 使用Content-Type = application / x-www-form-urlencoded发送POST请求时,POST值为空 - POST value is empty when Sending POST request with Content-Type = application/x-www-form-urlencoded PHP在非POST请求期间未解析x-www-form-urlencoded数据 - PHP not parsing x-www-form-urlencoded data during non-POST requests ionic和php之间的application / x-www-form-urlencoded - application/x-www-form-urlencoded between ionic and php 将 x-www-form-urlencoded 解码为数组 - Decoding x-www-form-urlencoded to array 如何使用内容类型为“ application / x-www-form-urlencoded”的PHP curl发送原始JSON? - How can I send a raw JSON using PHP curl with the content type “application/x-www-form-urlencoded”? 的file_get_contents( &#39;PHP://输入&#39;); 与application / x-www-form-urlencoded; - file_get_contents('php://input'); with application/x-www-form-urlencoded; PHP应用程序/ x-www-form-urlencoded错误地解码了特殊字符 - PHP application/x-www-form-urlencoded decodes special character wrongly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM