Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP, how can I detect that input vars were truncated due to max_input_vars being exceeded?

I know that an E_WARNING is generated by PHP

PHP Warning: Unknown: Input variables exceeded 1000

But how can I detect this in my script?

like image 710
Andy Avatar asked Nov 30 '22 05:11

Andy


2 Answers

A "close enough" method would be to check if( count($_POST, COUNT_RECURSIVE) == ini_get("max_input_vars"))

This will cause a false positive if the number of POST vars happens to be exactly on the limit, but considering the default limit is 1000 it's unlikely to ever be a concern.

like image 50
Niet the Dark Absol Avatar answered Dec 04 '22 03:12

Niet the Dark Absol


count($_POST, COUNT_RECURSIVE) is not accurate because it counts all nodes in the array tree whereas input_vars are only the terminal nodes. For example, $_POST['a']['b'] = 'c' has 1 input_var but using COUNT_RECURSIVE will return 3.

php://input cannot be used with enctype="multipart/form-data". http://php.net/manual/en/wrappers.php.php

Since this issue only arises with PHP >= 5.3.9, we can use anonymous functions. The following recursively counts the terminals in an array.

function count_terminals($a) {
  return is_array($a)
           ? array_reduce($a, function($carry, $item) {return $carry + count_terminals($item);}, 0)
           : 1;
}
like image 33
Dan Chadwick Avatar answered Dec 04 '22 04:12

Dan Chadwick