This function will allow you to take $_GET, $_POST, and $_COOKIE variables,
filter them (is alpha, is number, is email, etc.)
and returns the variable in scope.
<?php
function assign($variable,$type,$restrictions) {
$temp='';
switch($type) {
case 'get': $temp = @$_GET[$variable]; break;
case 'post': $temp = @$_POST[$variable]; break;
case 'cookie': $temp = @$_COOKIE[$variable]; break;
}
switch($restrictions) {
case 'alpha': preg_match("/([a-zA-Z ,\.]+)/",$temp,$match); break;
case 'alphanum': preg_match("/([a-zA-Z0-9 ,\.]+)/",$temp,$match); break;
case 'num': preg_match("/([0-9]+)/",$temp,$match); break;
case 'email': preg_match("/^(([a-zA-Z0-9_-]*\.*)*[a-zA-Z0-9_-]+@[a-zA-Z0-
9_-]+(\.[a-zA-Z0-9_-]+)+)/",$temp,$match); break;
// add your own filters here
}
if($temp!='') {
global $$variable;
$$variable = $match[1];
return true;
}
return false;
}
?>
Example, assuming register_globals is OFF
<?php
printf("Before assigning the variable, it should be NOT be set.<br>" .
"The variable, 'test', is currently %s<hr>",
isset($test) ? 'set' : 'NOT set'
);
printf("Now assigning the variable, '\$test'...<br>");
$result = assign('test','get','alpha');
printf("Here's what the function has done:<br>" .
"The function returned: %s <br>" .
"The value of \$test is: %s <hr>",
$result ? 'TRUE' : 'FALSE',
$test
);