We all know we shouldn't trust user input. Any problems with this function or easier ways to do it?
// cleanse variables
function assign($variable,$type,$restrictions) {
$temp='';
switch($type) {
case 'get': $temp = $_GET[$variable]; break;
case 'post': $temp = $_POST[$variable]; break;
case 'request': $temp = $_REQUEST[$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;
case 'blob': $match[1] = $temp; break;
}
if($temp!='') {
global $$variable;
$$variable = $match[1];
return true;
}
return false;
}
// suppose you want to strip any non-alphanumeric stuff out of $_POST['username']
assign('username','post','alphanumeric');