note 55808 added to security.globals

From: Date: Sun, 14 Aug 2005 09:04:03 +0000
Subject: note 55808 added to security.globals
Groups: php.notes 
Request: Send a blank email to php-notes+get-93574@lists.php.net to get a copy of this message
I have found out a method which seems to me the best. I've written my own version of extract. It works as follows: <?php safe_extract($_POST, "post", array("param1", "param2")); // and now I can use the following variables: echo "param1 is <b>$post_param1_html</b><br />"; mysql_query("SELECT * FROM sometable WHERE something = '$post_param2_slashes'"); if($post_param1_unsafe != $post_param2_unsafe) // do something. ?> I think it to be more convenient than using the required functions manually and more safe than that, and surely much more safe than register_globals = On. The code is very simple. You can write your own version, of course, but I'll also show mine: <?php function make_variables($key, $value, $prefix) { $GLOBALS["{$prefix}_{$key}_unsafe"] = $value; $GLOBALS["{$prefix}_{$key}_slashes"] = addslashes($value); $GLOBALS["{$prefix}_{$key}_url"] = urlencode($value); $GLOBALS["{$prefix}_{$key}_html"] = htmlspecialchars($value); $GLOBALS["{$prefix}_{$key}_url_html"] = htmlspecialchars(urlencode($value)); } function safe_extract($array, $prefix, $keys) { if(count(array_diff(array_values($keys), array_keys($array))) != 0) return false; foreach($keys as $key) make_variables($key, $array[$key], $prefix); return true; } ?> ---- Manual Page -- http://www.php.net/manual/en/security.globals.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55808 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55808&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55808&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55808&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55808&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55808&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55808&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55808&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93574) next »