2

I seem to be hitting the limit on max_input_vars in a PHP script I'm working on. I know I can just increase the limit (which I did while I'm refactoring the script) but I don't want that to be the end solution.

The way I believe max_input_vars works is that each nested level of an array is allotted an amount of elements less than the value of max_input_vars.

So a two-dimensional array such as $array1[999][999] would be fine assuming the value of max_input_vars is set to 1000.

However a two dimensional array such as $array2[1010][2] would violate the value if it was set to 1000.

Is my understanding correct? If not, how does this setting really work?

Just to be complete, My POST string looks like this:

Note: I copied this right from my browser inspector under the "form data" section of the request. I took the URL encoded value and passed it through parse_str to make it readable.

array(172) {
  ["line_1"]=>
  string(11) "1_85021_1_2"
  ["line_2"]=>
  string(11) "1_85038_1_2"
  ...
  ["line_167"]=>
  string(11) "1_85077_1_2"
  ["QUANTITY"]=>
  array(167) {
    ["1_85021_1_2"]=>
    string(6) "118685"
    ["1_85038_1_2"]=>
    string(6) "237520"
    ...
    ...
  }
  ["DATE1"]=>
  array(167) {
    ["1_85021_1_2"]=>
    string(0) ""
    ["1_85038_1_2"]=>
    string(0) ""
    ...
    ...
  ["SPLIT"]=>
  array(167) {
    ["1_85021_1_2"]=>
    string(1) "2"
    ["1_85038_1_2"]=>
    string(1) "0"
    ...
    ...
  }
  ["DATE2"]=>
  array(166) {
    ["1_85021_1_2"]=>
    string(0) ""
    ["1_85038_1_2"]=>
    string(10) "08/08/2017"
    ...
    ...
  }
  ["COMMENT"]=>
  array(166) {
    ["1_85021_1_2"]=>
    string(0) ""
    ["1_85038_1_2"]=>
    string(6) "test21"
    ...
    ...
  }

For verbosity, the exact error I'm getting is here:

PHP Warning: Unknown: Input variables exceeded 1000. To increase the limit change max_input_vars in php.ini. in Unknown on line 0

d.lanza38
  • 2,525
  • 7
  • 30
  • 52
  • 2
    The best would be to pack data in JSON and pass it as one input variable. – Axalix Aug 07 '17 at 20:28
  • Yeah, that's not a bad idea. I'll probably go ahead with that. However, I'd still be very curious why what I have isn't working (assuming `max_input_vars` works how I think it works. And if it does not, I very much would like to understand how it does work. – d.lanza38 Aug 07 '17 at 20:30
  • 1
    `max_input_vars` is a number of input variables, which can be scalars or arrays. It's 1000 by-default, you can have 1000 arrays or 1000 strings - doesn't matter. What matters though is `post_max_size`, which is by-default 8MB (all data in-total). – Axalix Aug 07 '17 at 20:34
  • Not sure why this was down voted. It's be much appreciated if you could let me know how I can improve this question (and subsequently future questions as well). – d.lanza38 Aug 08 '17 at 12:19

1 Answers1

3

Each array element is it's own variable sent as, for a single dimension:

array1%5B%5D=1 & array1%5B%5D=1

And for two dimensions:

array1%5B1%5D%5B0%5D=1 & array1%5B2%5D%5B0%5D=1

So array1[999][999] WILL violate max_input_vars if set at 1000, because array1[0][0] thorough array1[0][999] is 1000 variables. In this case, adding array1[1][0] will put it over 1000 and generate:

Warning: Unknown: Input variables exceeded 1000. To increase the limit change max_input_vars in php.ini. in Unknown on line 0

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Okay, then my understanding is correct aside from me forgetting PHP is base 0. `array1[998][998]` should be fine, right? – d.lanza38 Aug 07 '17 at 21:14
  • 1
    Only if you start at `array1[998][0]` and go to `array1[998][998]` – AbraCadaver Aug 07 '17 at 21:16
  • 1
    Right, it doesn't matter if the sum of all of the elements in all of the dimensions exceeds 1000, so long as each individual dimension does not exceed 1000 elements. I wonder why I'm getting the error then. The largest dimension I have in the array is 172. Any idea? – d.lanza38 Aug 07 '17 at 21:23
  • 2
    Ah, I just saw your edit. So the 1000 variable limit IS across all array dimensions. Turns out I was just misunderstanding how the setting worked. Thank you very much for clarifying. – d.lanza38 Aug 08 '17 at 12:50
  • 2
    Yes, because the vars are not an array until PHP processes them they are individual vars. – AbraCadaver Aug 08 '17 at 15:37
  • @AbraCadaver This is a very good explanation. As an admin, I spent a lot of time wondering why PHP scripts like Wordpress require increasing max_input_vars to 5000. It makes sense only if it is HTML fields that are being counted, before PHP processes them as arrays (input variables, after all). After googling for 15 minutes I get the right answer in a small SO comment :) – site80443 Apr 26 '21 at 22:11