神秘的空 $_POST 数组

rae*_*kid 22 php

我有以下 HTML/PHP 页面:

<?php
if(empty($_SERVER['CONTENT_TYPE'])) {
    $type = "application/x-www-form-urlencoded";
    $_SERVER['CONTENT_TYPE'] = $type;
}

echo "<pre>";
var_dump($_POST);
var_dump(file_get_contents("php://input"));
echo "</pre>";
?>

<form method="post" action="test.php">
<input type="text" name="test[1]" />
<input type="text" name="test[2]" />
<input type="text" name="test[3]" />
<input type="submit" name="action" value="Go" />
</form>
Run Code Online (Sandbox Code Playgroud)

如您所见,表单将提交并且预期的输出是一个 POST 数组,其中一个数组包含填充的值和一个值为“Go”(按钮)的条目“action”。但是,无论我在字段中输入什么值;结果总是:

array(2) {
  ["test"]=>
  string(0) ""
  ["action"]=>
  string(2) "Go"
}
string(16) "test=&action=Go&"
Run Code Online (Sandbox Code Playgroud)

不知何故,名为 test 的数组被清空,“action”变量确实通过了。

我已经使用了 Firefox 的 Live HTTP Headers 扩展来检查 POST 字段是否被提交,他们确实提交了。来自 Live HTTP Headers 的相关信息(在文本框中填充了 a、b 和 c 作为值):

Content-Type: application/x-www-form-urlencoded
Content-Length: 51
test%5B1%5D=a&test%5B2%5D=b&test%5B3%5D=c&action=Go
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么会这样?我被这个吓坏了,它已经花费了我很多时间......

更新:

我们已经在不同的服务器上尝试过这个,在 Windows 机器上它确实有效,在 PHP 版本 5.2.4(使用 Suhosin)的 Ubuntu 服务器上,它没有。它甚至可以在不同的服务器上运行,同样使用 Ubuntu 和相同的 PHP 版本,也安装了 Suhosin。

我对这两个文件进行了比较,这是输出 ( diff php.ini phps.ini):

270c270
< memory_limit = 32M
---
> memory_limit = 16M      ; Maximum amount of memory a script may consume (16MB)
415c415
< variables_order = "EGCSP"
---
> variables_order = "EGPCS"
491d490
< include_path = ".:"
1253a1253,1254
> extension=mcrypt.so
>
Run Code Online (Sandbox Code Playgroud)

在这个 phps.ini 是来自它工作的服务器的那个,而 php.ini 是当前的那个。这里好像没有问题吧?

小智 0

我不确定但是有

name="test[1]"
Run Code Online (Sandbox Code Playgroud)

等等可能会混淆 php。我将输入名称更改为 test_1、test_2 并看看会发生什么。

  • @haavee:不,PHP *广告*此用法:http://www.php.net/manual/en/faq.html.php#faq.html.arrays (7认同)