The PHP website created by its users!
Home
»
PHP » Simple escape function
|
Simple escape function
| Author | SGBoise |
| Description | Escapes variables from external sources such as POST and GET. It will recursively escape arrays also.
You can require it to make sure that the variable is an integer.
$id = cleaner($id, true); |
| Rating | (3 votes) |
PHP Code
function cleaner($data, $requireint = false) {
// Check for null
if (!isset($data) || empty($data)) {
return $data;
}
// This must be an integer or else return false
if ($requireint == true) {
if (!is_numeric($data)) {
//todo notify webmaster
die("The integer value contains invalid data");
} else {
return (int)$data;
}
}
if(is_array($data)) {
$ret = array();
foreach($data as $key=>$value) {
$ret[$key] = cleaner($value);
}
return $ret;
} else {
if(!is_numeric($data)) {
// oh what the heck throw in the NULL code
if($data != 0 && empty($data)) {
$data = 'NULL';
} else {
if(get_magic_quotes_gpc()) {
$data = stripslashes($data);
}
$data = mysql_real_escape_string($data);
}
}
return $data;
}
}
Comments
No comments posted yet.
|