PHP limit on maximum form fields

We had an interesting issue to debug at work today.  One of the screens in our application features a form with a whole lot of checkboxes.  It’s in the access control module, where the administrator of the system can manage user permissions for each module of the system.  Here’s the screenshot just to give you an idea.

This is not the pretties user interface (yet), but it sort of works.  Every module can be expanded or collapsed, and inside each modules all checkboxes can be checked and unchecked easily.

The problem that we had wasn’t user interface related.  It was something else.  The number of modules and permission checkboxes varies from system to system.  This is based on each particular system setup.  Now, on some of these systems, it was reported that some permissions are not being saved.

The problem is not new, but it was slowly escalating with more and more clients reporting it.  So, today we dived into it and found the cause of it.   Since PHP 5.3.9 there is a new runtime configuration setting: max_input_vars, with default value of 1.000:

max_input_varsinteger

How many input variables may be accepted (limit is applied to $_GET, $_POST and $_COOKIE superglobal separately). Use of this directive mitigates the possibility of denial of service attacks which use hash collisions. If there are more input variables than specified by this directive, an E_WARNING is issued, and further input variables are truncated from the request.

So, on the systems, where the form contains more than a thousand checkboxes, PHP was only bringing in the first thousand and skipping the rest, causing not all permissions being saved properly.

Increasing the value in runtime configuration is one way to solve it.  But since we have a rather dynamic system and don’t always control the runtime configuration (client hosting), we opted for a different solution.  As per this StackOverflow thread, it’s a much more future-proof solution to combine the values into a single field.  Either simple concatenate, or JSON-encode the values on form submit, and send them all as a single field value.  Then just split or JSON-decode on the server before processing and you are done.

P.S.: The extra bit that made the troubleshooting so much more difficult was that for some reason we were not seeing the PHP warning in logs.

 

On Semantic Versioning

First of all, you should know that versioning is important.  Even the worst versioning practices provide more value than no versioning at all.  At work, we are big fans of the Semantic Versioning, and we use it for all our projects, plugins, and libraries.  And I think, you should do too.

In general, Semantic Versioning works great for us.  But there were a few bumps recently, with more and more libraries dropping support for PHP 5.6 and requiring PHP 7.  I can’t blame them – after all PHP 5.6 has reached its end of life quite a while ago.

It’s not what the maintainers do, but how they do it that I have an issue with.  I’ve been thinking about writing a blog post on the subject for a few month now.  Never got to it.  And yesterday I came across this blog post by Paul Jones, which is so much better than whatever I was about to say.  Paul explains the problem in detail and suggests the “System” addendum to Semantic Versioning:

I opine that requiring a change in the public environment into which a package is installed is just as major an incompatibility as introducing a breaking change to the public API of the package. To cover that case, I offer the following as a draft addendum to the SemVer spec:

  • If the package consumer has to change a publicly-available system resource to upgrade a package, then the package upgrade is not backwards-compatible with the existing system, and the package SHOULD receive a major version bump.

Using “SHOULD” makes this rule somewhat less strict than the MUST of a major version bump when changing the package API.

Coming back to PHP 5.6 vs PHP 7, that would suggest that maintainers who drop support for PHP 5.6 SHOULD bump up the MAJOR release of the library.   And I wholeheartedly agree with that!

P.S.: For those of you who don’t or can’t use Semantic Versioning for whatever reason, checkout Paul’s blog post on Semantic Versioning vs. Romantic Versioning.

spf13-vim : The Ultimate Vim Distribution

spf13-vim is an amazing Vim distribution with cross-platform configuration and a large bundle of plugins, aimed at programmers in all sorts of languages.  Those of you just starting with Vim, or using a very basic configuration, give this one a spin.  And for the rest of us, ancient farts with 10+ year old configurations, this distribution provides plenty of inspiration for plugins and configuration options to try and play with.

I’ve seen a variety of Vim distributions and bundles over the years, but nothing came close to this amazing setup.  Very well done!