Skip to content

Instantly share code, notes, and snippets.

@matthewpoer
Last active August 29, 2015 14:24
Show Gist options
  • Save matthewpoer/a0538d0952ac96957a7e to your computer and use it in GitHub Desktop.
Save matthewpoer/a0538d0952ac96957a7e to your computer and use it in GitHub Desktop.
Attempt to demonstrate a potential PHP Bug wherein a string of data that includes an equals sign (=), when json_encode()'d twice and delivered to a web server, will be interpreted as a split in a data array.
<?php
echo "<h1>JSON Array to Double Array PHP Bug(?)</h1>";
echo "<p>Attempt to demonstrate a potential PHP Bug wherein a string of data that includes an equals sign (=), when json_encode()'d twice and delivered to a web server, will be interpreted as a split in a data array.</p>\n";
$array = array(
'one' => 'one is one',
'two' => 'two = two',
);
$encoded_once = json_encode($array);
$top_array = array(
'requests' => $encoded_once,
);
$encoded_twice = json_encode($top_array);
echo "<h2>Building the request</h2>";
echo "Child Data: \n";
var_dump($array);
echo "The Child Data array is json_encode()'d and appended to the Parent Data.<br>\n";
echo "Parent Data: \n";
var_dump($top_array);
echo "The Parent Data is again json_encode()'d and and will be sent to the server.<br>\n";
var_dump($encoded_twice);
echo "Sending to service ...<br />\n\n";
$url = 'http://localhost/~matthewpoer/EqualsTesting/ApiServer.php';
/*
* begin cURL setup
*/
$curl_request = curl_init($url);
/*
* the Request will be different for differnet
*/
curl_setopt($curl_request, CURLOPT_POST, 1);
/*
* set cURL options, including a 10 minute limit on waiting for the
* server's response and 2 minute limit waiting on its connection
*/
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, false);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($curl_request, CURLOPT_TIMEOUT, 600);
curl_setopt($curl_request, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $encoded_twice);
$result = curl_exec($curl_request);
echo "<h2>Server Results:</h2>";
echo $result;
curl_close($curl_request);
<?php
var_dump($_REQUEST);
<h1>JSON Array to Double Array PHP Bug(?)</h1><p>Attempt to demonstrate a potential PHP Bug wherein a string of data that includes an equals sign (=), when json_encode()'d twice and delivered to a web server, will be interpreted as a split in a data array.</p>
<h2>Building the request</h2>Child Data:
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=2)</i>
'one' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'one is one'</font> <i>(length=10)</i>
'two' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'two = two'</font> <i>(length=9)</i>
</pre>The Child Data array is json_encode()'d and appended to the Parent Data.<br>
Parent Data:
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=1)</i>
'requests' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'{&quot;one&quot;:&quot;one is one&quot;,&quot;two&quot;:&quot;two = two&quot;}'</font> <i>(length=38)</i>
</pre>The Parent Data is again json_encode()'d and and will be sent to the server.<br>
<pre class='xdebug-var-dump' dir='ltr'><small>string</small> <font color='#cc0000'>'{&quot;requests&quot;:&quot;{\&quot;one\&quot;:\&quot;one is one\&quot;,\&quot;two\&quot;:\&quot;two = two\&quot;}&quot;}'</font> <i>(length=61)</i>
</pre>Sending to service ...<br />
<h2>Server Results:</h2><pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=1)</i>
'{&quot;requests&quot;:&quot;{\&quot;one\&quot;:\&quot;one_is_one\&quot;,\&quot;two\&quot;:\&quot;two_' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>' two\&quot;}&quot;}'</font> <i>(length=9)</i>
</pre>
JSON Array to Double Array PHP Bug(?)
Attempt to demonstrate a potential PHP Bug wherein a string of data that includes an equals sign (=), when json_encode()'d twice and delivered to a web server, will be interpreted as a split in a data array.
Building the request
Child Data:
array (size=2)
'one' => string 'one is one' (length=10)
'two' => string 'two = two' (length=9)
The Child Data array is json_encode()'d and appended to the Parent Data.
Parent Data:
array (size=1)
'requests' => string '{"one":"one is one","two":"two = two"}' (length=38)
The Parent Data is again json_encode()'d and and will be sent to the server.
string '{"requests":"{\"one\":\"one is one\",\"two\":\"two = two\"}"}' (length=61)
Sending to service ...
Server Results:
array (size=1)
'{"requests":"{\"one\":\"one_is_one\",\"two\":\"two_' => string ' two\"}"}' (length=9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment