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

查看:21
本文介绍了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

谁能解释这种奇怪的 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.

例如原始帖子的片段:

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

Key 't_dodparam' 在 post 中,它有 key 198.其余参数丢失(ex t_dodparam2 在 post 中,但它没有像 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天全站免登陆