Mysteriously empty $_POST array

There's a number of possible reasons why the post array could be empty - chances have that it comes back to human/developer error. I experienced this exact issue when upgrading from PHP 5.2 to 5.4, it was simple but it took hours of troubleshooting to find the bug. In our config.php file we had the below statement to process $_POST arrays:

if (!get_magic_quotes_gpc()) {
    if (isset($_POST)) {
        foreach ($_POST as $key => $value) {
            $_POST[$key] =  trim(addslashes($value));
        }
    }

Magic quotes was once on, and in PHP versions up to 5.2 the above worked fine but anything above version 5.2 it won't process and an empty array is returned.

If you don't have error_reporting() switched on I suggest that you do and I'm sure you'll be able to troubleshoot the issue.

You should also check for deprecated system features, like "magic_quotes" as using them simply fails to return results. I hope this helps. Best of luck. JCS :)


Does it work without the explicit indices? Try:

<form method="post" action="test.php">
<input type="text" name="test[]" />
<input type="text" name="test[]" />
<input type="text" name="test[]" />
<input type="submit" name="action" value="Go" />
</form>

There are bug reports on this or similar issues in PHP's bugtracker:

  • http://bugs.php.net/bug.php?id=49945
  • http://bugs.php.net/bug.php?id=45043

Unfortunately it doesn't mention a solution, but you could try to set another CONTENT_TYPE or no content type at all.