php如何做sql过滤
SQL注入进击指的是经由过程构建特别的输入作为参数传入Web应用顺序,而这些输入大都是SQL语法里的一些组合,经由过程实行SQL语句进而实行进击者所要的操纵,其主要原因是顺序没有仔细地过滤用户输入的数据,以致不法数据侵入体系。因而,在实行sql语句前,肯定要对用户输入的数据举行过滤处置惩罚。
防备sql注入的函数,过滤掉那些不法的字符,进步sql安全性,同时也能够过滤XSS的进击。
function filter($str) { if (empty($str)) return false; $str = htmlspecialchars($str); $str = str_replace( '/', "", $str); $str = str_replace( '"', "", $str); $str = str_replace( '(', "", $str); $str = str_replace( ')', "", $str); $str = str_replace( 'CR', "", $str); $str = str_replace( 'ASCII', "", $str); $str = str_replace( 'ASCII 0x0d', "", $str); $str = str_replace( 'LF', "", $str); $str = str_replace( 'ASCII 0x0a', "", $str); $str = str_replace( ',', "", $str); $str = str_replace( '%', "", $str); $str = str_replace( ';', "", $str); $str = str_replace( 'eval', "", $str); $str = str_replace( 'open', "", $str); $str = str_replace( 'sysopen', "", $str); $str = str_replace( 'system', "", $str); $str = str_replace( '$', "", $str); $str = str_replace( "'", "", $str); $str = str_replace( "'", "", $str); $str = str_replace( 'ASCII 0x08', "", $str); $str = str_replace( '"', "", $str); $str = str_replace( '"', "", $str); $str = str_replace("", "", $str); $str = str_replace(">", "", $str); $str = str_replace("<", "", $str); $str = str_replace("<SCRIPT>", "", $str); $str = str_replace("</SCRIPT>", "", $str); $str = str_replace("<script>", "", $str); $str = str_replace("</script>", "", $str); $str = str_replace("select","",$str); $str = str_replace("join","",$str); $str = str_replace("union","",$str); $str = str_replace("where","",$str); $str = str_replace("insert","",$str); $str = str_replace("delete","",$str); $str = str_replace("update","",$str); $str = str_replace("like","",$str); $str = str_replace("drop","",$str); $str = str_replace("DROP","",$str); $str = str_replace("create","",$str); $str = str_replace("modify","",$str); $str = str_replace("rename","",$str); $str = str_replace("alter","",$str); $str = str_replace("cas","",$str); $str = str_replace("&","",$str); $str = str_replace(">","",$str); $str = str_replace("<","",$str); $str = str_replace(" ",chr(32),$str); $str = str_replace(" ",chr(9),$str); $str = str_replace(" ",chr(9),$str); $str = str_replace("&",chr(34),$str); $str = str_replace("'",chr(39),$str); $str = str_replace("<br />",chr(13),$str); $str = str_replace("''","'",$str); $str = str_replace("css","'",$str); $str = str_replace("CSS","'",$str); $str = str_replace("<!--","",$str); $str = str_replace("convert","",$str); $str = str_replace("md5","",$str); $str = str_replace("passwd","",$str); $str = str_replace("password","",$str); $str = str_replace("../","",$str); $str = str_replace("./","",$str); $str = str_replace("Array","",$str); $str = str_replace("or 1='1'","",$str); $str = str_replace(";set|set&set;","",$str); $str = str_replace("`set|set&set`","",$str); $str = str_replace("--","",$str); $str = str_replace("OR","",$str); $str = str_replace('"',"",$str); $str = str_replace("*","",$str); $str = str_replace("-","",$str); $str = str_replace("+","",$str); $str = str_replace("/","",$str); $str = str_replace("=","",$str); $str = str_replace("'/","",$str); $str = str_replace("-- ","",$str); $str = str_replace(" -- ","",$str); $str = str_replace(" --","",$str); $str = str_replace("(","",$str); $str = str_replace(")","",$str); $str = str_replace("{","",$str); $str = str_replace("}","",$str); $str = str_replace("-1","",$str); $str = str_replace("1","",$str); $str = str_replace(".","",$str); $str = str_replace("response","",$str); $str = str_replace("write","",$str); $str = str_replace("|","",$str); $str = str_replace("`","",$str); $str = str_replace(";","",$str); $str = str_replace("etc","",$str); $str = str_replace("root","",$str); $str = str_replace("//","",$str); $str = str_replace("!=","",$str); $str = str_replace("$","",$str); $str = str_replace("&","",$str); $str = str_replace("&&","",$str); $str = str_replace("==","",$str); $str = str_replace("#","",$str); $str = str_replace("@","",$str); $str = str_replace("mailto:","",$str); $str = str_replace("CHAR","",$str); $str = str_replace("char","",$str); return $str; }
越发轻便的防备sql注入的要领(引荐运用这个):
if (!get_magic_quotes_gpc()) // 推断magic_quotes_gpc是不是为翻开 { $post = addslashes($name); // magic_quotes_gpc没有翻开的时刻把数据过滤 } $name = str_replace("_", "\_", $name); // 把 '_'过滤掉 $name = str_replace("%", "\%", $name); // 把' % '过滤掉 $name = nl2br($name); // 回车转换 $name= htmlspecialchars($name); // html标记转换 return $name;
PHP防XSS 防SQL注入的代码
/** * 过滤参数 * @param string $str 接收的参数 * @return string */ static public function filterWords($str) { $farr = array( "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU", "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU", "/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dump/is" ); $str = preg_replace($farr,'',$str); return $str; } /** * 过滤接收的参数或许数组,如$_GET,$_POST * @param array|string $arr 接收的参数或许数组 * @return array|string */ static public function filterArr($arr) { if(is_array($arr)){ foreach($arr as $k => $v){ $arr[$k] = self::filterWords($v); } }else{ $arr = self::filterWords($v); } return $arr; }
在防备被注入进击时,常会用到函数:htmlspecialchars()和addslashes() 、trim()函数。这两个函数都是对特别字符举行转义。
1)addslashes()作用及运用
addslashes()一般用于防备sql注入,它可对经由过程get,post和cookie通报过来的参数的单引号和双引号已null前加“\”举行转义
如:如变量$str=$_POST["str"];的值为:bb' or 1='1。经由过程addslashes()函数过滤后会变成:bb\' or 1=\'1;
2)htmlspecialchars()作用及运用
htmlspecialchars()也是对字符举行转义,与addslashes()差别的是htmlspecialchars()是将特别字符用援用实体替代。
如<script>alert('xss')</script>经由过程htmlspecialchars()过滤后为<script>alert('xss')</script>
3)addslashes()与htmlspecialchars()的区分
除了两个函数的转义体式格局差别外,它们的运用也差别。
addslashes()经由过程用于防备sql语句注入,在实行sql语句前对经由过程get、post和cookie通报来的参数中的单引号,双引号,\ 和null举行转义。
但sql实行胜利后,插进去到数据库中的数据是不带有转义字符\的。这是假如插进去到数据库中的是一些js剧本,当这些剧本被读取出来时照样会被实行。
这时候我们可对读取出来的数据运用htmlspecialchars()举行过滤,防止实行被注入的剧本。
更多PHP相干学问,请接见ki4网!
以上就是php如何做sql过滤的细致内容,更多请关注ki4网别的相干文章!