Register Globals Alternative

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Register Globals Alternative

Post 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.
Last edited by mpetrovich on Sun Jun 08, 2008 7:36 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Register Globals Alternative

Post by Weirdan »

It doesn't seem to handle arrays in incoming data.
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Re: Register Globals Alternative

Post 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'];
Post Reply