Search code examples
phpurlsquare-bracket

PHP truncates parameter name after squared brackets?


If the parameter name of an URL contains squared brackets (no matter if they are url-encoded or not), any following character is ignored by PHP and is not made available to the script (e.g. via $_GET).

Example request:

...showget.php?xxx%5B1%5Dyyy=42

$_GET:

Array
(
    [xxx] => Array
        (
            [1] => 42
        )
)  

As you can see, "yyy" didn't made it. ^^

(tested in PHP 5.3.28 & 5.5.10)

  1. Does somebody know if such URLs are even syntactically valid?
  2. Is this behaviour intended and documented (could not find anything) or should it rather be considered as a bug within PHP?
  3. If intended: Can i change the respective behaviour by changing a special setting or so?

Thanks!


Solution

  • This is intended behaviour. As you saw in your example, PHP builds arrays from GET parameters if it can, that is, if it finds square brackets in the variable name. There's a FAQ entry showing how that can sometimes be useful.

    In your case, PHP sees xxx[1]yyy=42 as xxx[1]=42 which becomes an array.

    As far as I know, PHP's query string parsing can not be changed, but you could use $_SERVER['QUERY_STRING'] and parse that yourself.