php防sql注入程序代码

放到公用调用文件(如conn数据库链接文件),对所有get或post的数据进行过滤特殊字符串,以实现简单有效的sql注入过滤。php初学者,欢迎批评指点 谢谢!

[代码]简单函数加判断

function inject_check($sql_str) {
return eregi(‘select|insert|and|or|update|delete|\’|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile’, $sql_str);
}
if (inject_check($_server[‘query_string’])==1 or inject_check(file_get_contents(“php://input”))==1){
//echo “警告 非法访问!”;
header(“location: error.php”);
}

2. [代码](2013年12月23日更新)结合了大家的指点 改成这样 大家觉得如何?感谢大家继续批评指教!(不区分大小写的)

function inject_check($sql_str) {
return preg_match(‘/select|insert|and|or|update|delete|\’|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/i’, $sql_str);
}
if (inject_check($_server[‘query_string’])==1 or inject_check(file_get_contents(“php://input”))==1){
//echo “警告 非法访问!”;
header(“location: error.php”);
exit;
}

Posted in 未分类

发表评论