php $_POST 数组在表单提交时为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1282909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:52:16  来源:igfitidea点击:

php $_POST array empty upon form submission

phparraysformspost

提问by tiltem

I have a custom CMS i've built that works perfectly on my dev box (Ubuntu/PHP5+/MySQL5+).

我有一个自定义的 CMS,它在我的开发箱(Ubuntu/PHP5+/MySQL5+)上完美运行。

I just moved it up to the production box for my client and now all form submissions are showing up as empty $_POST arrays.

我只是将它移到我客户的生产框,现在所有表单提交都显示为空的 $_POST 数组。

I found a trick to verify the data is actually being passed using file_get_contents('php://input');and the data is showing up fine there -- the $_POST/$_REQUESTarrays are always empty.

我发现了一个技巧来验证数据实际上是通过使用传递的,file_get_contents('php://input');并且数据在那里显示得很好—— $_POST/$_REQUEST数组总是空的。

I've also verified the content-type headers are correct as well via firebug (application/x-www-form-urlencoded; charset=utf-8).

我还通过 firebug ( application/x-www-form-urlencoded; charset=utf-8)验证了内容类型标头也是正确的。

This issue is happening regardless of whether a form is submitting via AJAX or a regular form submit.

无论表单是通过 AJAX 提交还是常规表单提交,都会发生此问题。

Any help is greatly appreciated!

任何帮助是极大的赞赏!

回答by tiltem

I know this question was about POST via a Form, but came here looking for answers for similar issue when POSTing with JSON content-type. Found the answer and wanted to share it as it cost me much time.

我知道这个问题是关于通过表单发布的,但来到这里是为了在使用 JSON 内容类型发布时寻找类似问题的答案。找到答案并想分享它,因为它花费了我很多时间。

When using JSON content-type the $_POST array will not populate (only with multi-part forms I believe)

使用 JSON 内容类型时,$_POST 数组不会填充(我相信仅适用于多部分表单)

Here is what did work to correct the issue:

以下是纠正问题的方法:

$rest_json = file_get_contents("php://input");
$_POST = json_decode($rest_json, true);

hope this helps someone!

希望这有助于某人!

回答by tiltem

Here's another possible cause -- my form was submitting to domain.com without the WWW. and I had set up an automatic redirect to add the "WWW." The $_POST array was getting emptied in the process. So to fix it all I had to do was submit to www.domain.com

这是另一个可能的原因——我的表单在没有 WWW 的情况下提交到 domain.com。我已经设置了一个自动重定向来添加“WWW”。$_POST 数组在此过程中被清空。所以为了解决这个问题,我所要做的就是提交到 www.domain.com

回答by Randy Kilwag

I had a similar problem. Turned out to be a simple fix. In the form I had

我有一个类似的问题。原来是一个简单的修复。在我有的表格中

<form action="directory" method="post">

<form action="directory" method="post">

where directory was the name of... the directory. My POST array was totally empty. When I looked at the url in my browser, it was displayed with a forward slash on the end.

其中目录是...目录的名称。我的 POST 数组完全是空的。当我在浏览器中查看 url 时,它显示的末尾带有一个正斜杠。

Adding the forward slash to the end of my action did the trick -

在我的动作末尾添加正斜杠就成功了 -

<form action="directory/" method="post">

<form action="directory/" method="post">

My $_POST array was full again!

我的 $_POST 数组又满了!

回答by MrMage

Make sure that, in php.ini:

确保在 php.ini 中:

  • track_vars(it's only available on very old PHP versions) is set to On
  • variables_ordercontains the letter P
  • post_max_sizeis set to a reasonable value (e.g. 8 MB)
  • (if using suhosin patch) suhosin.post.max_varsand suhosin.request.max_varsare large enough.
  • track_vars(它仅适用于非常旧的 PHP 版本)设置为 On
  • variables_order包含信 P
  • post_max_size设置为合理的值(例如 8 MB)
  • (如果使用 suhosin 补丁)suhosin.post.max_vars并且suhosin.request.max_vars足够大。

I suppose the second suggestion of mine will solve your problem.

我想我的第二个建议会解决你的问题。

回答by Marcos

I've found that when posting from HTTP to HTTPS, the $_POSTcomes empty. This happened while testing the form, but took me a while until I realize that.

我发现当从 HTTP 发布到 HTTPS 时,它$_POST是空的。这发生在测试表单时,但我花了一段时间才意识到这一点。

回答by GUIR

I came across a similar yet slightly different issue and it took 2 days to understand the issue.

我遇到了一个类似但略有不同的问题,花了 2 天的时间来理解这个问题。

  • In my case also POST array was empty.

  • Then checked with file_get_contents('php://input'); and that was also empty.

  • 在我的情况下,POST 数组也是空的。

  • 然后检查 file_get_contents('php://input'); 那也是空的。

Later I found that browser wasnt asking confirmation for resubmitting form data after If I refresh the page loaded after POST submission. It was directly refreshing page. But when I changed form URL to a different one it was passing POST properly and asked for resubmitting data when attempted to refresh page.

后来我发现如果我刷新POST提交后加载的页面后,浏览器没有要求确认重新提交表单数据。它是直接刷新页面。但是,当我将表单 URL 更改为不同的 URL 时,它正确地传递了 POST 并在尝试刷新页面时要求重新提交数据。

Then I checked what is wrong with actual URL . There were no fault with URL, however it was pointing to a folder without index.php in URL and I was checking POST at index.php.

然后我检查了实际 URL 有什么问题。URL 没有错误,但是它指向一个 URL 中没有 index.php 的文件夹,我正在检查 index.php 上的 POST。

Here I doubted the redirection from / to /index.php causes POST data to be lost and tested URL with appending index.php to the URL.

在这里,我怀疑从 / 到 /index.php 的重定向会导致 POST 数据丢失并通过将 index.php 附加到 URL 来测试 URL。

That Worked.

那行得通。

Posted it here so someone would find it helpful.

把它贴在这里,所以有人会觉得它有帮助。

回答by Luke A. Leber

Having the enable_post_data_readingsetting disabled will cause this. According to the documentation:

具有enable_post_data_reading设置禁用会导致此问题。根据文档:

enable_post_data_reading

Disabling this option causes $_POST and $_FILES not to be populated. The only way to read postdata will then be through the php://input stream wrapper. This can be useful to proxy requests or to process the POST data in a memory efficient fashion.

enable_post_data_reading

禁用此选项会导致不填充 $_POST 和 $_FILES。读取 postdata 的唯一方法是通过 php://input 流包装器。这对于代理请求或以内存高效的方式处理 POST 数据非常有用。

回答by Sunday G Akinsete

If you are posting to a index.php file in a directory for example /api/index.php make sure in your form you specify the full directory to the file e.g

如果您要发布到目录中的 index.php 文件,例如 /api/index.php 请确保在您的表单中指定文件的完整目录,例如

This

这个

<form method="post" action="/api/index.php"> 
</form>

OR

或者

<form method="post" action="/api/"> 
</form>

works.

But this fails

作品。

但这失败了

<form method="post" action="/api"> 
</form>

回答by Ivan

<form action="test.php" method="post">
                        ^^^^^^^^^^^^^

Okay, this was stupid and I will be embarassing myself in public, but I knocked up a little test script for something in PHP and when my $_POSTarray was empty, StackOverflow is the first place I looked and I didn't find the answer I needed.

好吧,这太愚蠢了,我会在公共场合让自己难堪,但是我在 PHP 中编写了一些测试脚本,当我的$_POST数组为空时,StackOverflow 是我查看的第一个地方,但我没有找到我需要的答案.

I had only written

我只写过

<form action="test.php">

and forgotten to specify the method as being POST!

忘记指定方法为POST

I am sure someone will snigger, but if this helps someone else who does the same thing, then I don't mind! We all do it from time to time!

我相信有人会窃笑,但如果这有助于其他做同样事情的人,那么我不介意!我们都时不时地这样做!

回答by Ivan

Don't have an elegant solution at this point but wanted to share my findings for the future reference of others who encounter this problem. The source of the problem was 2 overriden php values in an .htaccess file. I had simply added these 2 values to increase the filesize limit for file uploads from the default 8MB to something larger -- I observed that simply having these 2 values in the htaccess file at all, whether larger or smaller than the default, caused the issue.

在这一点上没有一个优雅的解决方案,但想分享我的发现,以供将来遇到此问题的其他人参考。问题的根源是 .htaccess 文件中的 2 个覆盖的 php 值。我只是简单地添加了这 2 个值,以将文件上传的文件大小限制从默认的 8MB 增加到更大的大小——我观察到,在 htaccess 文件中仅仅拥有这 2 个值,无论是大于还是小于默认值,都会导致问题.

php_value post_max_size xxMB
php_value upload_max_filesize xxMB

I added additional variables to hopefully raise the limits for all the suhosin.post.xxx/suhosin.upload.xxx vars but these didn't have any effect with this problem unfortunately.

我添加了额外的变量,希望提高所有 suhosin.post.xxx/suhosin.upload.xxx 变量的限制,但不幸的是,这些对这个问题没有任何影响。

In summary, I can't really explain the "why" here, but have identified the root cause. My feeling is that this is ultimately a suhosin/htaccess issue, but unfortunately one that I wasn't able to resolve other than to remove the 2 php overridden values above.

总之,我无法真正解释这里的“为什么”,但已经确定了根本原因。我的感觉是,这最终是一个 suhosin/htaccess 问题,但不幸的是,除了删除上面的 2 个 php 覆盖值之外,我无法解决这个问题。

Hope this helps someone in the future as I killed a handful of hours figuring this out. Thanks to all who took the time to help me with this (MrMage, Andrew)

希望这对未来的人有所帮助,因为我花了几个小时来解决这个问题。感谢所有花时间帮助我的人(MrMage,Andrew)