PHP缺少一些$ _POST值,但在php://input中存在 [英] PHP some $_POST values missing but are present in php://input

查看:59
本文介绍了PHP缺少一些$ _POST值,但在php://input中存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的html表单(包含带行的表,其中包含多个输入),我需要通过POST请求将其提交到PHP脚本. 问题是PHP的$ _POST超全局变量中没有某些值,这些值不存在.

I have a very big html form (containing table with rows, which contain multiple inputs), which i need to submit to PHP script via POST request. The problem is some values don't come through and are absent in PHP's $_POST superglobal.

我检查(使用Firebug扩展名)这些值实际上是由浏览器发送到服务器的.

I checked (using Firebug extension) that the values are actually sent to server by the browser.

$ _ POST被填充,但是一些值丢失了.

$_POST gets populated, but some values are just missing.

我使用以下命令检查了原始请求:

I checked what is raw request using:

$raw_post = file_get_contents('php://input');

,返回的字符串具有值.它们只是不解析为$ _POST数组.我注意到的一件奇怪的事情是,似乎php://input值在经过一定长度后被切掉了,其余的字符串却没有传递到$ _POST.

and the string returned has the values. They are just not parsed into $_POST array. The strange thing i noticed is, it seems that the php://input values are cut after some length, and rest of the string does not come through to $_POST.

我考虑过post_max_size和memory_limit并将它们设置为较大的值:

I thought about post_max_size and memory_limit and set them to large values:

memory_limit = 256M
post_max_size = 150M

但是根据php文档,如果发出的请求大于post_max_size,则$ _POST不应包含任何值.

but according to php documentation $_POST should not contain any values if request made is bigger than post_max_size.

由于表单和请求的尺寸过大,我无法在此处发布它,但是我可以发布用于调试问题的php脚本:

Due to big size of form and request I cannot post it here, but i can post php script i used to debug the problem:


var_dump($file = file_get_contents('php://input'));
var_dump($_POST);
//... then i parsed the $file

服务器版本:Apache/2.2.9(Debian)
PHP版本:PHP 5.3.2-0.dotdeb.2

Server version: Apache/2.2.9 (Debian)
PHP version: PHP 5.3.2-0.dotdeb.2

enyone能否解释这种奇怪的PHP行为的原因,在处理表单时我应该怎么做(更改php设置,代码?)以使用$ _POST数组?

Can enyone explain reason of such strange PHP behaviour, and what should i do (change php settings, code?) to use $_POST array while processing form?

要清楚:不仅缺少值. $ _POST也不包含这些键.

To be clear: not only the values are missing. $_POST does not contain these keys either.

e.x.原始帖子的片段:

e.x. fragment of raw post:

t_dodparam%5B198%5D=&t_dodparam2%5B198%5D=&t_kolejnosc%5B198%5D=199&n_indeks=201&n_wartosc=testtesttest

键"t_dodparam"在后,它具有键198.其余参数丢失(例如t_dodparam2在后,但是它没有键198,并且在$ _POST中没有诸如n_wartosc这样的键)

Key 't_dodparam' is in post and it has key 198. The rest of parameters are missing (e.x. t_dodparam2 is in post, but it has no such key as 198, and there is no such key as n_wartosc in $_POST)

推荐答案

PHP修改包含字符空格,点,方括号和其他字符的字段,以与不赞成使用的register_globals兼容

PHP modifies fields containing the characters space, dot, open square bracket and others to be compatible with with the deprecated register_globals

您可以在以下评论中找到许多解决方法: PHP:来自外部源的变量

you can find a lot of workarounds in the comments here: PHP: Variables From External Sources

例如(由POSTer发表评论):

For Exampe (comment by POSTer):

<?php
//Function to fix up PHP's messing up POST input containing dots, etc.
function getRealPOST() {
    $pairs = explode("&", file_get_contents("php://input"));
    $vars = array();
    foreach ($pairs as $pair) {
        $nv = explode("=", $pair);
        $name = urldecode($nv[0]);
        $value = urldecode($nv[1]);
        $vars[$name] = $value;
    }
    return $vars;
}
?>

这篇关于PHP缺少一些$ _POST值,但在php://input中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆