在PHP中,如何检测由于超出max_input_vars而截断的输入变量?

And*_*ndy 4 php apache security

我知道它E_WARNING是由PHP生成的

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

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

Nie*_*sol 10

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

如果POST变量的数量恰好在限制上,这将导致误报,但考虑到默认限制为1000,它不太可能成为一个问题.


Dan*_*ick 7

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;
}
Run Code Online (Sandbox Code Playgroud)