4

我知道 anE_WARNING是由 PHP 生成的

PHP 警告:未知:输入变量超过 1000

但是如何在我的脚本中检测到这一点?

4

6 回答 6

12

“足够接近”的方法是检查if( count($_POST, COUNT_RECURSIVE) == ini_get("max_input_vars"))

如果 POST 变量的数量恰好在限制范围内,这将导致误报,但考虑到默认限制为 1000,这不太可能成为问题。

于 2012-08-29T01:55:54.827 回答
4

count($_POST, COUNT_RECURSIVE)不准确,因为它计算了数组树中的所有节点,而 input_vars 只是终端节点。例如,$_POST['a']['b'] = 'c'有 1 个 input_var 但使用COUNT_RECURSIVE将返回 3。

php://input不能与enctype="multipart/form-data". http://php.net/manual/en/wrappers.php.php

由于此问题仅在 PHP >= 5.3.9 时出现,我们可以使用匿名函数。以下递归计算数组中的终端。

function count_terminals($a) {
  return is_array($a)
           ? array_reduce($a, function($carry, $item) {return $carry + count_terminals($item);}, 0)
           : 1;
}
于 2015-01-31T13:26:29.510 回答
3

对我有用的是这个。首先,我把它放在我的脚本/处理程序/前端控制器的顶部。这是错误将被保存的地方(或者 $e0 将为空,这没关系)。

$e0 = error_get_last();

然后我运行了一堆其他的处理,引导我的应用程序,注册插件,建立会话,检查数据库状态——很多事情——不管超过这个条件我都可以完成。然后我检查这个 $e0 状态。如果它不为空,我们有一个错误,所以我退出(假设 App 是一个大类,里面有很多你的魔法)

if (null != $e0) {
    ob_end_clean(); // Purge the outputted Warning
    App::bail($e0); // Spew the warning in a friendly way
}

为您自己的状态调整和调整错误处理程序。

注册错误处理程序不会捕获此条件,因为它在您的错误处理程序注册之前就存在。

检查输入变量计数是否等于最大值是不可靠的。

上面的 $e0 将是一个数组,type => 8,line => 0; 该消息将明确提及 input_vars,因此您可以通过正则表达式匹配来创建一个非常狭窄的条件并确保对特定情况的肯定识别。

另请注意,根据 PHP 规范,这是警告而不是错误。

于 2015-11-24T06:13:07.163 回答
1
function checkMaxInputVars()
{
    $max_input_vars = ini_get('max_input_vars');
    # Value of the configuration option as a string, or an empty string for null values, or FALSE if the configuration option doesn't exist
    if($max_input_vars == FALSE)
        return FALSE;

    $php_input = substr_count(file_get_contents('php://input'), '&');
    $post = count($_POST, COUNT_RECURSIVE);

    echo $php_input, $post, $max_input_vars;

    return $php_input > $post;
}

echo checkMaxInputVars() ? 'POST has been truncated.': 'POST is not truncated.';
于 2012-08-30T19:54:10.960 回答
0

error_get_last()在你的脚本中尽快调用(在你有机会导致错误之前,因为它们会掩盖这个错误。)在我的测试中,如果适用,max_input_vars 警告将会出现。

这是我的 max_input_vars 设置为 100 的测试脚本:

<?php
if (($error = error_get_last()) !== null) {
    echo 'got error:';
    var_dump($error);
    return;
}
unset($error);

if (isset($_POST['0'])) {
    echo 'Got ',count($_POST),' vars';
    return;
}
?>
<form method="post">
<?php
for ($i = 0; $i < 200; $i++) {
    echo '<input name="',$i,'" value="foo" type="hidden">';
}
?>
<input type="submit">
</form>

达到 var 限制时的输出:

got error:
array
  'type' => int 2
  'message' => string 'Unknown: Input variables exceeded 100. To increase the limit change max_input_vars in php.ini.' (length=94)
  'file' => string 'Unknown' (length=7)
  'line' => int 0

使用 PHP 5.3.10 和 Apache 2.2.22 在 Ubuntu 上测试。

我会犹豫是否明确检查此错误字符串、稳定性(他们可以更改它)和一般的 PHP 良好实践。我更喜欢将所有 PHP 错误转换为异常,就像这样(单独的子类可能有点过头了,但我喜欢这个例子,因为它允许@错误抑制。)它会有点不同,error_get_last()但应该很容易适应。

我不知道是否还有其他可能被此方法捕获的执行前错误。

于 2014-02-06T11:16:08.090 回答
0

类似的东西怎么样:

$num_vars = count( explode( '###', http_build_query($array, '', '###') ) );

你可以对$_POST, $_GET, $_COOKIE,不管重复。

仍然不能被认为是 100% 准确,但我想它非常接近它。

于 2017-03-27T15:52:50.280 回答