note 55786 deleted from function.get-magic-quotes-gpc by philip

From: Date: Tue, 27 Jun 2006 06:39:04 +0000
Subject: note 55786 deleted from function.get-magic-quotes-gpc by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-114345@lists.php.net to get a copy of this message
Note Submitter: dinesh at dinsoft dot net ---- 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 ?>

« previous php.notes (#114345) next »