php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32121 WDDX does not properly serialize variables
Submitted: 2005-02-26 20:47 UTC Modified: 2005-03-21 01:00 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: jbeall at heraldic dot us Assigned:
Status: No Feedback Package: Arrays related
PHP Version: 5.0.3 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
36 + 26 = ?
Subscribe to this entry?

 
 [2005-02-26 20:47 UTC] jbeall at heraldic dot us
Description:
------------
If you give a form input a name that should be a numerically indexed array, such as

<input type='text' name='fname[1]' />

It is not possible to directly access the variable via $_GET, $_POST, or $_REQUEST.

Dumping the entire contents of the request using e.g. print_r reveals that the form data is submitted, but it cannot be accessed via e.g. $_POST['fname']['1'] or $_POST['fname'][1]

Presumably this happens because the PHP engine is doing type juggling.  When a request variable is submitted and looks like an array, the PHP puts it in an array and indexes the value at the string value '1'.  However, when you try to access $_POST['fname']['1'] it sees that '1' could be an integer, converts to an integer transparently to the developer, and of course there is nothing stored at $_POST['fname'][1].  The data was stored at $_POST['fname']['1'].

For whatever reason, embedding the variable in a string appears to solve the problem.  E.g., "{$_POST['test'][1]}" works

Reproduce code:
---------------
echo "<pre>"; // So we can use \n for formatting
echo <<<EOT
<form method='GET' action='{$_SERVER['PHP_SELF']}'>
	<input type='text' name='test[1]' value='{$_REQUEST['test'][1]}'/>
	<input type='submit'/>
</form>
EOT;

echo '('.gettype($test[1]).') value='.$test[1]."\n";
echo '('.gettype($test[1]).') value='.$test['1']."\n";
echo "But it works if we embed in a string ---> {$_REQUEST['test'][1]}\n";
var_dump($_REQUEST['test']);

// Put a string variable in the textbox and submit.
// If using GET method, URL might look like 
// bugTest.php?test%5B1%5D=any+string+here

Expected result:
----------------
(string) value=any string here
(string) value=any string here
But it works if we embed in a string ---> any string here
Array
(
    [1] => any string here
)

Actual result:
--------------
(NULL) value=
(NULL) value=
But it works if we embed in a string ---> any string here
Array
(
    [1] => any string here
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-27 12:30 UTC] tony2001@php.net
<form method="post" action="index.php">
<input type="text" name="test[1]" value="bla-bla"/>
<input type="submit">
</form>

<?
var_dump($_POST);
var_dump($_POST['test'][1]);
?>
Works fine here, debug your code.
 [2005-02-27 13:09 UTC] jbeall at heraldic dot us
You are right.  I must not be truly understanding what is causing the problem (I am definitely having the problem that I cannot access array values from a form), but it must be something other than the name='fname[1]' field name.

I was trying to simplify my code down to less than 20 lines for the post and I was missing $_REQUEST.

What I may have to do is simply save is serialize the variable that I have that is causing me so much trouble and post a link to that.  I'm really at a loss at this point as to what might be the cause since it is apparently not the form problem.  Sorry for messing up the reproduce code.
 [2005-03-01 01:10 UTC] jbeall at heraldic dot us
I have tracked down the problem to WDDX not properly serializing arrays.  Consider the following:

$data = array(1=>"First value",2=>"Second Value",3=>"Third Value");

$wddxPacket = wddx_serialize_value($data);
$deserialized = wddx_deserialize($wddxPacket);

echo gettype($deserialized[1])." {$deserialized[1]}\n";
var_dump($deserialized);

It outputs:

NULL 
array(3) {
  ["1"]=>
  string(11) "First value"
  ["2"]=>
  string(12) "Second Value"
  ["3"]=>
  string(11) "Third Value"
}

You will note that the indices are now string values rather than integers.  This is apparently because the array begins at an index other than 0.  The expected output is of course:

string First value
array(3) {
  [1]=>
  string(11) "First value"
  [2]=>
  string(12) "Second Value"
  [3]=>
  string(11) "Third Value"
}

Perhaps this should be filed as a separate bug report, or is already a known bug?

As far as changing the status, the only statuses that I am able to set it to are "open" and "closed" - "feedback" is not listed as one of my options.
 [2005-03-09 00:37 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip

And do NOT reply anything before you've tried the snapshot, thank you.

 [2005-03-21 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 13:01:30 2024 UTC