MySQL防SQL注入

1,mysql_real_escape_string()函数已经不安全,可以利用编码的漏洞来实现输入任意密码就能登录服务器的注入攻击
2,使用拥有Prepared Statement机制的PDO和MYSQLi来代替mysql_query(注:mysql_query自 PHP 5.5.0 起已废弃,并在将来会被移除)
【注:PreparedStatement使用预编译机制,在创建PreparedStatement对象时就需要将sql语句传入,传入的过程中参数要用?替代,这个过程回导致传入的sql被进行预编译,然后再调用PreparedStatement的setXXX将参数设置上去,由于sql语句已经经过了预编译,再传入特殊值也不会起作用了。
而且PreparedStatement使用了预编译机制,sql语句在执行的过程中效率比Statement要高。】
3,自己定义函数进行验证【正则表达式:(|’|(%27)|;|(%3b)|=|(%3d)|(|(%28)|)|(%29)|(/)|(%2a%2f)|+|(%2b)|<|(%3c)|>|(%3e)|(–)|[|%5b|]|%5d)】
eg:
function safeRequest($ParaName KaTeX parse error: Expected '}', got 'EOF' at end of input: …/ if(ParaType == 1){
KaTeX parse error: Expected group after '^' at position 9: re = "/[^̲\w+]/";
}
else{
$re = "/(|’|(%27)|;|(%3b)|=|(%3d)|(|(%28)|)|(%29)|(/
)|(%2a%2f)|+|(%2b)|<|(%3c)|>|(%3e)|(–)|[|%5b|]|%5d)/";
}

      if(preg_match($re , $ParaName)>0){
          echo ("参数不符合要求,请重新输入");
           return 0; 
      }else{
          return 1; 
      }

}

猜你喜欢

转载自blog.csdn.net/qq_31424153/article/details/84032649