Page 1 of 1

Register Globals Alternative

Posted: Sat Jun 07, 2008 9:04 pm
by mpetrovich
I have been using the following code to create variables from Post and Get, and of course, avoid "Register Globals" problems.

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) : '';
}
Here is an example of how to create variables from a post, using the above.

Code: Select all

SetPost('name address1 address2 city state zip submit');
This will create seven variables: $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.

Re: Register Globals Alternative

Posted: Sun Jun 08, 2008 5:33 am
by Weirdan
It doesn't seem to handle arrays in incoming data.

Re: Register Globals Alternative

Posted: Sun Jun 08, 2008 7:56 am
by mpetrovich
Yes, the functions previously provided do not allow arrays. That requires a multidimensional array which needs a different structure. Below is a function to get a posted associative array. With this function, you need to provide the array name. What is interesting is that you do not need to define the array beforehand for this to work ($myarray = array() is not needed).

Code: Select all

function SetPostArray($ArrayName,$str,$ModStr='ST')
{
    $VARS = explode(' ',$str);
    foreach ($VARS as $PV) {
        $GLOBALS[$ArrayName][$PV] = isset($_POST[$ArrayName][$PV])? 
             TransformContent($_POST[$ArrayName][$PV],$ModStr) : '';
    }
}
Try this.

Code: Select all

$_POST['myarray']['test']='fun';
SetPostArray('myarray','test');
echo $myarray['test'];