note 28091 added to function.get-magic-quotes-gpc

From: Date: Wed, 01 Jan 2003 21:21:24 +0000
Subject: note 28091 added to function.get-magic-quotes-gpc
Groups: php.notes 
Request: Send a blank email to php-notes+get-41551@lists.php.net to get a copy of this message
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. ;) -- http://www.php.net/manual/en/function.get-magic-quotes-gpc.php http://master.php.net/manage/user-notes.php?action=edit+28091 http://master.php.net/manage/user-notes.php?action=delete+28091 http://master.php.net/manage/user-notes.php?action=reject+28091

« previous php.notes (#41551) next »