php - 使用 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 上,但我可以'不知道如何让事情与默认值一起工作。
解决方案
我能够解决这个问题。我发现这是我们的 Node 代理和 php 的交互。
在我们的 Node 代码中,我们使用了“body-parser”npm 包。我们有一行使用中间件处理表单数据,如下所示:
app.use(bodyParser.urlencoded({extended: true}));
该中间件在请求被代理之前将表单数据转换为 JSON,从而阻止 PHP 获取数据。
希望这对将来可能遇到类似情况的人有所帮助。
推荐阅读
- xcode - 将现有 SwiftUI 应用程序移至新的 Xcode 12 SwiftUI 应用程序 Live Cycle
- flutter - Flutter:根据所选类别在 GridView 内显示值
- ios - 引用 mainstoryboard 中的 tableview
- conv-neural-network - caffe 模型中的形状大小不规则
- firebase - 什么是类似于 firebase 托管的谷歌云产品?
- javascript - 如何动态修改框架元素的物理体属性
- scala - 将如何隐式扩展定义
- node.js - 有没有办法在 npm start 后在终端中获取用户输入?
- c# - Xamarin.Forms 图像导致网格行高度过度增加
- node.js - 如何在nodejs中的一个if循环中检查变量是否存在及其值