note 108552 added to function.json-decode
| From: | Anonymous | Date: | Sun, 06 May 2012 07:08:08 +0000 |
| Subject: | note 108552 added to function.json-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-187490@lists.php.net to get a copy of this message | ||
<?php
include_once('json_decode.php'); // #100740 's json_decode()
var_dump(json_decode('"$json"'));
var_dump(json_decode('"$omething"'));
var_dump(json_decode('date("c")'));
?>
In this code, a php without built-in json_decode() prints:
string(7) ""$json""
string(0) ""
string(25) "2012-05-05T00:00:00+00:00"
expecting:
string(5) "$json"
string(9) "$omething"
NULL
They can be too vulnerable. To avoid them, it should be like
below. Since this code is still not perfect, when you have to
decode a JSON that might be invalid, you had better use a
JSON library like PEAR or Zend's, or upgrade your php.
<?php
if (!function_exists('json_decode')) {
function json_decode($json) {
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($json); $i++) {
if (!$comment) {
if (($json[$i] == '{') || ($json[$i] == '[')) {
$out .= 'array(';
}
elseif (($json[$i] == '}') || ($json[$i] == ']')) {
$out .= ')';
}
elseif ($json[$i] == ':') {
$out .= '=>';
}
elseif ($json[$i] == ',') {
$out .= ',';
}
elseif ($json[$i] == '"') {
$out .= '"';
}
/*elseif (!preg_match('/\s/', $json[$i])) {
return null;
}*/
}
else $out .= $json[$i] == '$' ? '\$' : $json[$i];
if ($json[$i] == '"' && $json[($i-1)] != '\\') $comment =
!$comment;
}
eval($out. ';');
return $x;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 27.141.192.142
----
Manual Page -- http://www.php.net/manual/en/function.json-decode.php
Edit -- https://master.php.net/note/edit/108552
Del: integrated -- https://master.php.net/note/delete/108552/integrated
Del: useless -- https://master.php.net/note/delete/108552/useless
Del: bad code -- https://master.php.net/note/delete/108552/bad+code
Del: spam -- https://master.php.net/note/delete/108552/spam
Del: non-english -- https://master.php.net/note/delete/108552/non-english
Del: in docs -- https://master.php.net/note/delete/108552/in+docs
Del: other reasons-- https://master.php.net/note/delete/108552
Reject -- https://master.php.net/note/reject/108552
Search -- https://master.php.net/manage/user-notes.php