I create a list of variable names using a space-delimited string passed to a function, with an optional transform string.
First, here are the functions.
Code: Select all
function TransformContent($var,$list)
{
//List: S=strip tags, T=trim, Q=remove quotes, H=htmlentities, A=addslashes
if (empty($list)) return $var;
$list = strtoupper($list);
if (strpos($list,'S')!==false) $var = strip_tags($var);
if (strpos($list,'Q')!==false) $var = str_replace(array('"',"'"),'',$var);
if (strpos($list,'H')!==false) $var = htmlentities($var);
if (strpos($list,'T')!==false) $var = trim($var);
if (strpos($list,'A')!==false) $var = addslashes($var);
return $var;
}
function SetPost($str,$ModStr='ST')
{
$VARS = explode(' ',$str);
foreach ($VARS as $PV) $GLOBALS[$PV] = isset($_POST[$PV])? TransformContent($_POST[$PV],$ModStr) : '';
}
function SetGet($str,$ModStr='ST')
{
$VARS = explode(' ',$str);
foreach ($VARS as $PV) $GLOBALS[$PV] = isset($_GET[$PV])? TransformContent($_GET[$PV],$ModStr) : '';
}
function SetBoth($str,$ModStr='ST')
{
$VARS = explode(' ',$str);
foreach ($VARS as $PV) $GLOBALS[$PV] = isset($_REQUEST[$PV])? TransformContent($_REQUEST[$PV],$ModStr) : '';
}Code: Select all
SetPost('name address1 address2 city state zip submit');Now, you will notice in my functions, all the variables will be defined. If they are not set from a post, they will be returned as empty strings. I like to have all the variables defined, so I do not run into undefined variable problems. So, I can check using if($var) or if(!empty($var)).
Notice also, that I called SetPost, so $_GET variables will be ignored. I could call, SetGet, if I wanted $_GET variables, or SetBoth, for both.
The transform function defaults to trimming the variable strings, and removing HTML tags. So, that is a quick way to process the variables. Any other transforms could be added.
I like using the space-delimited string because it is easy to code, but this could be written with any delimiter.