note 28091 deleted from function.get-magic-quotes-gpc by aidan

From: Date: Tue, 06 Jul 2004 06:20:40 +0000
Subject: note 28091 deleted from function.get-magic-quotes-gpc by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72699@lists.php.net to get a copy of this message
Note Submitter: ian@NO_SPAM.verteron.net ---- A nice solution is to use a reference if gpc_magic_quotes are off, so the code will be nice and fast when they are (no array copy will be performed) - and since this is now the default, only legacy code will be slow - and removes the problem commented on above concerning how other scripts will behave if you modify $_GET, $_POST and so on. I'll leave the reader to sort out deep traversal to handle PHP arrays and so on: ------------------------------------------------------ function & gpc_quotes(&$array) { if(get_magic_quotes_gpc()) { $result = array(); foreach($array as $key => $val) { $result[$key] = stripslashes($array); } return $result; } else { return $array; } } $get = & gpc_quotes($_GET); $post = & gpc_quotes($_POST); /* I now use $get to actually access vars, which will just be a reference if magic quotes are off, but a whole new array if they're on. I won't be writing to this array, because results would be unpredictable - but a tidy script shouldn't be writing to $_GET, $_POST anyway. */ ------------------------------------------------------ I sorta wrote this on the spot for demo purposes, but I reckon it should work okay. ;)

« previous php.notes (#72699) next »