note 55786 added to function.get-magic-quotes-gpc
| From: | dinesh at dinsoft dot net | Date: | Sat, 13 Aug 2005 01:59:23 +0000 |
| Subject: | note 55786 added to function.get-magic-quotes-gpc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93532@lists.php.net to get a copy of this message | ||
Here is a little code that you can put in the beginning of your script to undo the job of the magic
quotes.
Note that this code does not follow what the doc says because the doc is wrong.
( " If magic_quotes_sybase is on, a single-quote is escaped with a single-quote instead of a
backslash if magic_quotes_gpc or magic_quotes_runtime are enabled. " )
In fact, (tested in PHP 4.4.0) , magic_quotes_sybase works only if magic_quotes_gpc is enabled.
(magic_quotes_runtime doesn't have any effect)
So here is the code (which should manage all 3 magic quotes types):
<?php
// Undo what magic quotes possibly did
function magic_quotes_stripslashes_b($a) { // Back
$r=array();
foreach($a as $k=>$v) {
if (!is_array($v)) $r[stripslashes($k)] = stripslashes($v);
else $r[stripslashes($k)] = magic_quotes_stripslashes_b($v);
}
return $r;
}
function magic_quotes_stripslashes($a) { // Top
$r=array();
foreach($a as $k=>$v) {
if (!is_array($v)) $r[$k] = stripslashes($v);
else $r[$k] = magic_quotes_stripslashes_b($v);
}
return $r;
}
function magic_quotes_stripquotes_b($a) { // Back
$r=array();
foreach($a as $k=>$v) {
if (!is_array($v)) $r[str_replace('\'\'','\'',$k)] =
str_replace('\'\'','\'',$v);
else $r[str_replace('\'\'','\'',$k)] =
magic_quotes_stripquotes_b($v);
}
return $r;
}
function magic_quotes_stripquotes($a) { // Top
$r=array();
foreach($a as $k=>$v) {
if (!is_array($v)) $r[$k] = str_replace('\'\'','\'',$v);
else $r[$k] = magic_quotes_stripquotes_b($v);
}
return $r;
}
if (get_magic_quotes_gpc()) {
if (@ini_get('magic_quotes_sybase')=='1') {
$_GET = magic_quotes_stripquotes($_GET);
$_POST = magic_quotes_stripquotes($_POST);
$_COOKIE = magic_quotes_stripquotes($_COOKIE);
$_REQUEST = magic_quotes_stripquotes($_REQUEST);
}
else {
$_GET = magic_quotes_stripslashes($_GET);
$_POST = magic_quotes_stripslashes($_POST);
$_COOKIE = magic_quotes_stripslashes($_COOKIE);
$_REQUEST = magic_quotes_stripslashes($_REQUEST);
}
}
@set_magic_quotes_runtime(0); // switch it off
?>
----
Manual Page -- http://www.php.net/manual/en/function.get-magic-quotes-gpc.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55786
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55786&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55786&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55786&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55786&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55786&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55786&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55786&report=yes
Search -- http://master.php.net/manage/user-notes.php