-1

is there a way in PHP to set the max size of a single POST variable (not the whole POST size as we do with post_max_size)?

EDIT: I was looking for a way to limit the size of a single POST param, not the whole POST. But it seems that you can't do that in PHP and you have to use post_max_size (therefore you set the limit of the whole POST).

tonix
  • 6,671
  • 13
  • 75
  • 136
  • do you mean a general configuration as post_max_size in php.ini? Or you just want to validate the size after the submit? As far as I know, the first option does not exist. –  Mar 28 '17 at 13:48
  • This seems like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. What are you actually trying to do? – Styphon Mar 28 '17 at 13:48
  • Possible duplicate of [PHP: whats the total length of a post global variable?](http://stackoverflow.com/questions/2276759/php-whats-the-total-length-of-a-post-global-variable) – Pieter van den Ham Mar 28 '17 at 13:50
  • This may be helpful for you [PHP: Measure size in kilobytes of a object/array?](http://stackoverflow.com/questions/3072452/php-measure-size-in-kilobytes-of-a-object-array) – Sriram G Mar 28 '17 at 13:56
  • @Styphon I am trying to limit the size of a single POST parameter when sending a long string back to the server. As I undertood, `post_max_size` limits the size of the whole POST request (no matter how much parameters you put into it, of course if don't hit `max_input_vars`), but I wanted to know if I can limit the size of a single parameter, ex. `$_POST['this_param_contains_a_long_long_string']` – tonix Mar 28 '17 at 13:56
  • How about using the [maxlength](https://developer.mozilla.org/en/docs/Web/HTML/Element/input#attr-maxlength) attribute? UTF-8 uses up to 4 bytes per character, just divide the max size you want by four and that's the maximum number of characters you should allow. – Styphon Mar 28 '17 at 14:12
  • @Styphon Yeah, that could be a solution, but in my specific case I needed to send back a long JSON string to the server (a JSON which I modify on the client side and add even more data to send back through AJAX). – tonix Mar 28 '17 at 15:01

1 Answers1

0

You need to change your php.ini file

post_max_size = 16M
upload_max_filesize = 16M
memory_limit = 128M

Change these value in php.ini if you've access to it, otherwise you can try to change them in an .htaccess file.

php_value upload_max_filesize 16M
php_value post_max_size 16M 

This will work only if the AllowOverride settings permit it. Otherwise, you've to ask to your hosting company.

Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35
  • It seems `post_max_size` is the only way. It's not a file by the way. I was working with `application/x-www-form-urlencoded` forms. – tonix Mar 28 '17 at 13:59
  • yeah it's the only way, you can't use **ini_set()** to change it, because `post_max_size` is used before your script is started, so the only way to set it, is editing .htaccess or php.ini files – Otávio Barreto Mar 28 '17 at 14:34