php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64325 Issue in automatic $_POST array handling
Submitted: 2013-02-28 16:09 UTC Modified: 2013-10-15 11:54 UTC
From: php at sygmoral dot com Assigned: laruence (profile)
Status: No Feedback Package: *General Issues
PHP Version: 5.4.12 OS: Debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at sygmoral dot com
New email:
PHP Version: OS:

 

 [2013-02-28 16:09 UTC] php at sygmoral dot com
Description:
------------
Php automatically puts POSTed name-value pairs with names ending in [] into arrays. Very handy feature! However, I notice issues when more of those square brackets are encountered. 

If I send a name like `save[line[]]`, then `save` will be an array and the first key in it will be `line[`, instead of `line[]`. It's not that I expect a second level of arrays - just that it doesn't strangely remove that last bracket. 

So suppose we have the tiny php script below, and I send this with e.g. jquery:
post_data = {'save[line[]]':'A line with text'}

Effectively, the raw POST data being sent is:
save%5Bline%5B%5D%5D=A+line+with+text

Test script:
---------------
print_r(
   $_POST['save']
);

Expected result:
----------------
POST: Array
(
    [line[]] => A line with text
)

Actual result:
--------------
POST: Array
(
    [line[] => A line with text
)

Patches

bug64325.patch (last revision 2013-03-01 03:29 UTC by laruence@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-28 18:22 UTC] reeze@php.net
"post_data = {'save[line[]]':'A line with text'}“


do you mean post_data = {'save[line][]':'A line with text'} ?
                                    ^^
is this you intention? 
array(
   'save' => 
        ['line' => 
             ['A line with text', 'maybe more lines']
        ]
); ?
 [2013-02-28 19:45 UTC] php at sygmoral dot com
Thank you for your reaction!
But no: I did in fact mean what I wrote. I realise it's a strange data structure, so here's a short explanation for it: the 'save' array holds a collection of html form elements that are not yet to be submitted, but should be saved temporarily into some other set of memory, so that upon the next visit, those temporary values can be easily inserted into the displayed form, without having been submitted. In other words, it's for a form that remembers its state throughout visits. 

So I send an object containing the name-value pairs in the form, and send that over POST. In the example used here, this results in one or more name-value pairs that are saved into the save array, as save['line[]'] = value. 

And that is the situation that triggers this bug, as in my original post. I'm sure there are other ways to achieve what I want, but I figured I'd report it since this does not look as if it's intended. 

Note that the example is a simplification of my application, where multiple 'single' and 'array' values are saved.
 [2013-03-01 03:29 UTC] laruence@php.net
PHP won't allow variables name to contains "[", "." etc , so I think this is 
really a narrow usage.

but, however I do believe there is a bug.

a patch will be attached. but I need to ask someone else's opinion before commit 
it.

thanks
 [2013-03-01 03:29 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2013-03-01 03:29 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug64325.patch
Revision:   1362108583
URL:        https://bugs.php.net/patch-display.php?bug=64325&patch=bug64325.patch&revision=1362108583
 [2013-03-01 03:30 UTC] laruence@php.net
-Package: Arrays related +Package: *General Issues -Assigned To: +Assigned To: laruence
 [2013-03-03 14:10 UTC] reeze@php.net
The attached patch may break form like: foo[bar][index]=test

after some research, this seems a undocumented behavior but not a bug.

only the field name `save` can not have '[' and other special chars.
the subkeys didn't required to be a legal variable name but '[]' are special
chars.
 [2013-03-03 14:28 UTC] laruence@php.net
I noticed, that's why I didn't going further, 

and I don't think it related to documented or not:
<?php

$array ["[]"] = "xxxx";
var_dump($array["[]"]);
 [2013-10-15 11:54 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2014-01-04 17:16 UTC] php at sygmoral dot com
So far I've actually circumvented the bug (or feature), but that makes my code look a bit weird as I'm accessing data like $arr["somkey["]. 

Here's a short case that shows exactly what happens: 

$str = "arr[weirdkey[]]=foo&arr[otherkey[]]=bar";
parse_str($str, $output);
var_dump($output);

I'll probably end up changing how my application works, but still, I feel like something is amiss in how it currently works. I feel like it should either keep track of the brackets and so use -everything- inside the outer brackets as the key for the array; or otherwise, if the parser simply stops at the first ] (which is fine), I feel like I should get a notice or warning or... something somewhere, since there's another ] before the =. It's like arr[key]]=foo, isn't that supposed to fail?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 07:01:28 2024 UTC