表单提交时,PHP的$_POST数组为空。

119

我有一个自定义的内容管理系统(CMS),在我的开发环境中(Ubuntu/PHP5+/MySQL5+)运行得非常完美。

我刚把它移到生产环境为我的客户服务,但现在所有的表单提交都显示为空的 $_POST 数组。

我发现了一个技巧来验证数据是否被传递,使用 file_get_contents('php://input'); ,数据在那里正常显示 -- 无论是 $_POST/$_REQUEST 数组总是空的。

我也通过 firebug 验证了正确的 content-type header (application/x-www-form-urlencoded; charset=utf-8)。

不管是通过 AJAX 还是普通的表单提交,这个问题都会发生。

非常感谢任何帮助!


3
请检查post_max_size的值是否设置为8M而不是8MB。如果设置错误,在最新版本中您可能看不到任何错误,但$_POST的大小将设置为0。 - Sergei Karpov
3
注意:如果缺少斜杠,Apache会进行301重定向。 - Leandro Bardelli
32个回答

1
另一个导致空的POST数组的简单原因可能是没有用</form>关闭表单,然后添加第二个<form>...</form>。当提交第二个表单时,POST数组将为空。

0

除了MRMage的帖子之外:

我不得不设置这个变量来解决一些$_POST变量(具有大于1000个项目的大型数组)消失的问题:

suhosin.request.max_vars = 2500

"request",而不是"post"才是解决方案...。

0

我从Mod Security收到了以下错误:

Access denied with code 500 (phase 2). Pattern match "((select|grant|delete|insert|drop|alter|replace|truncate|update|create|rename|describe)[[:space:]]+[A-Z|a-z|0-9|\*| |\,]+[[:space:]]+(from|into|table|database|index|view)[[:space:]]+[A-Z|a-z|0-9|\*| |\,]|UNION SELECT.*\'.*\'.*,[0-9].*INTO.*FROM)" at REQUEST_BODY. [file "/usr/local/apache/conf/modsec2.user.conf"] [line "345"] [id "300013"] [rev "1"] [msg "Generic SQL injection protection"] [severity "CRITICAL"]

当我移除了我的mod安全配置进行测试后,一切都按预期工作。现在我只需要修改我的规则以保持安全性,同时足够灵活以满足我的需求 :)


0
在我的情况下(在OVH共享服务器上的php页面),enctype="text/plain"不起作用($_POST和相应的$_REQUEST为空),而下面的其他示例可以正常工作。
<form action="?" method="post">
<!-- in this case, my google chrome 45.0.2454.101 uses -->
<!--     Content-Type:application/x-www-form-urlencoded -->
    <input name="say" value="Hi">
    <button>Send my greetings</button>
</form>

<form action="?" method="post" enctype="application/x-www-form-urlencoded">
    <input name="say" value="Hi">
    <button>Send my application/x-www-form-urlencoded greetings</button>
</form>

<form action="?" method="post"  enctype="multipart/form-data">
    <input name="say" value="Hi">
    <button>Send my multipart/form-data greetings</button>
</form>

<form action="?" method="post" enctype="text/plain"><!-- not working -->
    <input name="say" value="Hi">
    <button>Send my text/plain greetings</button>
</form>

`

更多信息请参见:method="post" enctype="text/plain" 不兼容?


0

也许不是最方便的解决方案,但我发现如果将表单的action属性设置为根域名,就可以访问并获取提交的变量。然而,如果将重写的URL设置为action,它就无法工作。


0

请确保在输入标签中使用 name="your_variable_name"。

我错误地使用了 id="your_variable_name"。

我花了很多时间来追踪这个错误。


0

这有点类似于 @icesar 所说的

但是我试图将内容发布到我的API,位于 site/api/index.php,只发布到 site/api,因为它会自己传递给 index.php。然而,这似乎导致某些问题,因为我的 $_POST 在处理中被清空了。直接发布到 site/api/index.php 就解决了这个问题。


0

当我迁移到新服务器时,我也遇到了同样的问题。在php.ini文件中设置post_max_size的值解决了我的问题。


0
我的问题是我使用HTML的<base>标签来更改测试站点的基本URL。一旦我从头部删除了该标签,$_POST数据就回来了。

0
在我的情况下,我在我的<form>标签中有target="_blank"。删除它解决了问题。
但是,如果您仍然需要在提交后打开一个新页面,只需将_blank替换为随机值,例如newtab(它可以是任何内容)。
之前:
<form target="_blank" action="api/" enctype="application/x-www-form-urlencoded" method="post">

之后:

<form target="newtab" action="api/" enctype="application/x-www-form-urlencoded" method="post">

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接