本文章提供這款防sql注入代碼是一款php sql雙過濾非法字符的函數,他可以根據用戶自定的防sql注入,先是過濾一些sql命令,再是就是把post get 過濾一次,最好驗證.
php防注入代碼如下:
- $arrfiltrate=array("update","delete","selert","drop","exec","cast","'","union");
- //出錯后要跳轉的url,不填則默認前一頁
- $strgourl="";
- //是否存在數組中的值
- function funstringexist($strfiltrate,$arrfiltrate){
- foreach ($arrfiltrate as $key=>$value){
- if (eregi($value,$strfiltrate)){
- return true;
- }
- }
- return false;
- }
- //合并$_post 和 $_get
- if(function_exists(array_merge)){
- $arrpostandget=array_merge($http_post_vars,$http_get_vars);
- }else{
- foreach($http_post_vars as $key=>$value){
- $arrpostandget[]=$value;
- }
- foreach($http_get_vars as $key=>$value){
- $arrpostandget[]=$value;
- }//開源代碼Vevb.com
- }
- //驗證開始
- foreach($arrpostandget as $key=>$value){
- if (funstringexist($value,$arrfiltrate)){
- echo " ";
- if (emptyempty($strgourl)){
- echo " ";
- }else{
- echo " ";
- }
- echo "<script>alert('系統檢測到非法字符!');history.back();</script>";
- exit();
- }
- }
新聞熱點
疑難解答