6

I am using PHP 5.3.8 with Apache 2.0. I am also using Symfony 2 but that's not where the script is failing. I have a form with array variables:

<form action="/app_dev.php/admin/position/create" method="post">
  <input type="text" id="po_name" name="po[name]" required="required" maxlength="50">
  <input type="text" id="po_role" name="po[role]" required="required" maxlength="20">
</form>

Directly in the app_dev.php file (to rule out Symfony from the problem) I do:

echo file_get_contents("php://input"); // outputs: po%5Bname%5D=Developer&po%5Brole%5D=ROLE_USER
var_dump($_POST); // outputs: array(1) { ["po"]=> array(1) { ["name"]=> string(9) "Developer" } }
die();

Basically it keeps only the first variable in the array. If I change the name of the variable from po[role] to ba[role] then $_POST outputs:

array(1) {
  ["po"]=> array(1) { ["name"]=> string(9) "Developer" },
  ["ba"]=> array(1) { ["role"]=> string(9) "ROLE_USER" }
}

Typical problems I have found that can cause this issue are due to the following php.ini configuration, I also give you what are my values:

max_execution_time = 30
max_input_time = 60 
max_input_nesting_level = 64
max_input_vars = 1000
post_max_size = 8M
upload_max_filesize = 2M
memory_limit = 128M

These values seems reasonable and I think don't cause the problem, but cannot be 100% sure.

I do not have suhosin installed as I've read it can cause similar problems too.

It is also similar to this problem but the solution given would require me to rewrite the HttpFoundation Symfony component.

Also I don't want to have to rewrite the form variable without and array (e.g. po[name] to po_name) as the form are automatically generated by Symfony and this seems to be a basic feature that PHP should be able to handle.

Does someone have any idea about this problem ?

PS: this is similar to the problem described in here. Plus the problem happens on the same version of Suse (SUSE Linux Enterprise Server 11 ).

Community
  • 1
  • 1
Sylvain
  • 542
  • 6
  • 20
  • Can you try it as a stand-alone script? I am wondering if some other part of your code is modifying `$_POST` since [your result is inconsistent with the documentation](http://www.php.net/manual/en/faq.html.php#faq.html.arrays). – jimp Oct 02 '12 at 04:32
  • 2
    I've never seen something like this.. `po[name] and po[role]` – Mr. Alien Oct 02 '12 at 04:57
  • +1 Mr Alien, I haven't either. I'm assuming you can't do it. Either way the browser sends the data the same its up to php to process it into an associative array. – shapeshifter Oct 02 '12 at 05:05
  • 1
    @Mr.Alien It's perfectly valid syntax. There's nothing wrong with using `name="po[name]"` etc. It should work exactly like the question assumes it will work. – user229044 Oct 02 '12 at 05:11
  • 2
    @Mr.Alien Then you simply haven't seen a lot. It works just fine. Usually, that is. – deceze Oct 02 '12 at 05:11
  • @Sylvain Can you test what this returns on your setup? http://codepad.org/MWQ8REar – deceze Oct 02 '12 at 05:15
  • @deceze really? I just use single name and [] and it returns an array, is that how you shoot an array with it's key? – Mr. Alien Oct 02 '12 at 05:16
  • @jimp I use app_dev.php as the standalone script. I var_dump($_POST) before any other code is called and then I call die(); So Symfony is not loaded at all when I display the content of $_POST. A friend tried with a standalone script on the same server and that does not work either. – Sylvain Oct 02 '12 at 05:17
  • 2
    @deceze interesting... `it returns array(1) { ["po"]=> array(1) { ["name"]=> string(9) "Developer" } }` – Sylvain Oct 02 '12 at 05:21
  • @Mr.Alien "Shoot"? Anyway, yes, that works. Some frameworks like Cake standardize on `data[ModelName][fieldname]` for all form names. – deceze Oct 02 '12 at 05:21
  • 1
    @Sylvain Hmm, then PHP's string parsing is apparently borked. I'd create/look for a bug report for that and/or try upgrading to the latest PHP version. – deceze Oct 02 '12 at 05:22
  • @deceze shoot kinda execute, and oh, I don't know anything about any framework as I program pure php..no frameworks, anyways thanks for the info :) – Mr. Alien Oct 02 '12 at 05:22
  • Try accessing the elements as follows: $_POST['po']['role'] and $_POST['po']['name'] and see if you can obtain your values that way. – kermit Oct 02 '12 at 05:28
  • 1
    did you ever test it on the other servers? just to be sure that it's not because of your local machine configuration ... – Mahdi Oct 02 '12 at 05:35
  • I have few module installed like mbstring but desactivating it, does not help. I also updated the version of pcre to 8.2. I am not sure what is responsible for that function. – Sylvain Oct 02 '12 at 05:35
  • Do you happen to have anything defined for `auto_prepend_file` in your `php.ini` file? – Phil Oct 02 '12 at 06:00
  • @Mahdi I tried on another server and everything works fine. It's for sure my server. – Sylvain Oct 02 '12 at 06:00
  • 1
    @Sylvain I would say instead of spending your time to debugging like this, just start with a fresh and basic server installation on your server (if it's possible), and then start installing/enabling other modules or components to see which one causes such a weird thing. – Mahdi Oct 02 '12 at 06:09
  • nothing defined in auto_prepend_file, the value is empty, and if unsetting it does not change anything – Sylvain Oct 02 '12 at 06:11
  • it would be nice if you could post a standalone, reproducable test case like in my answer below – eis Oct 02 '12 at 06:50
  • just a guess: What happens if you change the name-values to po[] twice to create a numerically indexed array? Does this work for you? – Jojo Oct 02 '12 at 11:30

2 Answers2

1

What you're proposing works fine for me. Full test code:

<html>
<body>
<pre>
<?php
if (isset($_POST) && !empty($_POST))
{
        echo file_get_contents("php://input");
        echo "\n\n";
        var_dump($_POST);
}
?>
</pre>
<form action="" method="post">
  <input type="text" id="po_name" name="po[name]">
  <input type="text" id="po_role" name="po[role]">
  <input type="submit">
</form>
</html>

Result snippet with 'one' and 'two' values:

po%5Bname%5D=one&po%5Brole%5D=two

array(1) {
  ["po"]=>
  array(2) {
    ["name"]=>
    string(3) "one"
    ["role"]=>
    string(3) "two"
  }
}

My PHP:

PHP Version 5.3.3-7+squeeze14
CGI/FastCGI
Apache/2.2
Suhosin Patch 0.9.9.1

No issues here, so it should clearly work. If this test code doesn't work for you, my best bet is a PHP bug in the version you have (or a bug in some seemingly unrelated functionality that hasn't been mentioned anywhere).

eis
  • 51,991
  • 13
  • 150
  • 199
1

We finally decided to update our version of PHP to a more recent version (5.3.15) and it works fine now. So this was for sure a problem with this 5.3.8, at least the version we had.

Sylvain
  • 542
  • 6
  • 20