note 71817 added to security.magicquotes.disabling

From: Date: Sun, 17 Dec 2006 20:20:05 +0000
Subject: note 71817 added to security.magicquotes.disabling
Groups: php.notes 
Request: Send a blank email to php-notes+get-120711@lists.php.net to get a copy of this message
PHP's magic quotes function has the strange behavior of not adding slashes to top level keys in GPC key/value pairs but adding the slashes in deeper level keys. To demonstrate, a URI of: example.php?a'b[c'd]=e'f produces: array("a'b" => array("c\'d" => "e\'f")) The current example for removing magic quotes does not do anything to keys, so after running stripslashes_deep, you would end up with: array("a'b" => array("c\'d" => "e'f")) Which, needless to say, is wrong. As if you had magic quotes off, it would have been: array("a'b" => array("c'd" => "e'f")) I have written a snippet of code compatible with PHP 4.0.0 and above that handles this correctly: if (get_magic_quotes_gpc()) { function undoMagicQuotes($array, $topLevel=true) { $newArray = array(); foreach($array as $key => $value) { if (!$topLevel) { $key = stripslashes($key); } if (is_array($value)) { $newArray[$key] = undoMagicQuotes($value, false); } else { $newArray[$key] = stripslashes($value); } } return $newArray; } $_GET = undoMagicQuotes($_GET); $_POST = undoMagicQuotes($_POST); $_COOKIE = undoMagicQuotes($_COOKIE); $_REQUEST = undoMagicQuotes($_REQUEST); } ---- Server IP: 203.121.145.99 Probable Submitter: 203.156.6.89 ---- Manual Page -- http://www.php.net/manual/en/security.magicquotes.disabling.php Edit -- https://master.php.net/note/edit/71817 Del: integrated -- https://master.php.net/note/delete/71817/integrated Del: useless -- https://master.php.net/note/delete/71817/useless Del: bad code -- https://master.php.net/note/delete/71817/bad+code Del: spam -- https://master.php.net/note/delete/71817/spam Del: non-english -- https://master.php.net/note/delete/71817/non-english Del: in docs -- https://master.php.net/note/delete/71817/in+docs Del: other reasons-- https://master.php.net/note/delete/71817 Reject -- https://master.php.net/note/reject/71817 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120711) next »