I found this piece of code here: http://php.net/manual/de/reserved.variables.get.php
Want to use it to make my code safer. I use quite a few $_GET var in my project.
Please, if possible I would like you professionals to have a look and see if this piece of code could be enhanced or has any problems.
There is a smart way to protect the $ _GET input from malicious injection and options for inserting default values:
<?php
// Smart GET function
public function GET($name=NULL, $value=false, $option="default")
{
$option=false; // Old version depricated part
$content=(!empty($_GET[$name]) ? trim($_GET[$name]) (!empty($value) && !is_array($value) ? trim($value) : false));
if(is_numeric($content))
return preg_replace("@([^0-9])@Ui", "", $content);
else if(is_bool($content))
return ($content?true:false);
else if(is_float($content))
return preg_replace("@([^0-9\,\.\+\-])@Ui", "", $content);
else if(is_string($content))
{
if(filter_var ($content, FILTER_VALIDATE_URL))
return $content;
else if(filter_var ($content, FILTER_VALIDATE_EMAIL))
return $content;
else if(filter_var ($content, FILTER_VALIDATE_IP))
return $content;
else if(filter_var ($content, FILTER_VALIDATE_FLOAT))
return $content;
else
return preg_replace("@([^a-zA-Z0-9\+\-\_\*\@\$\!\;\.\?\#\:\=\%\/\ ]+)@Ui", "", $content);
}
else false;
}
/*
DEFAULT: $_GET['page'];
SMART: GET('page'); // return value or false if is null or bad input
*/
?>
Source : http://php.net/manual/de/reserved.variables.get.php