我正在尝试编写一个通用的功能,用于对输入到Mysql数据库中的内容进行过滤。目前为止,这是我所拥有的:
function sanitize($input){
if(get_magic_quotes_qpc($input)){
$input = trim($input); // get rid of white space left and right
$input = htmlentities($input); // convert symbols to html entities
return $input;
} else {
$input = htmlentities($input); // convert symbols to html entities
$input = addslashes($input); // server doesn't add slashes, so we will add them to escape ',",\,NULL
$input = mysql_real_escape_string($input); // escapes \x00, \n, \r, \, ', " and \x1a
return $input;
}
}
如果我正确理解了
get_magic_quotes_qpc()
的定义,那么它是由php服务器设置的,可以自动转义字符,无需使用addslashes()
。我是否已经正确地同时使用了
addslashes()
和mysql_real_escape_string()
,并且有没有其他方法可以增加净化程度。谢谢。