PHP에서 특수 위험 문자 필터링 요약

高洛峰
풀어 주다: 2016-11-29 15:20:05
원래의
1712명이 탐색했습니다.

在网站中表单提交或url获取值我们都可能碰到一些安全问题,下面我总结了一些常用的过滤一些危险特殊字符的解决方法,一般,对于传进来的字符,php可以用addslashes函数处理一遍(要get_magic_quotes_gpc()为假才处理,不然就重复转义了!),这样就能达到一定程度的安全要求,比如这样,代码如下:

if (!get_magic_quotes_gpc()) {      

     add_slashes($_GET);      

     add_slashes($_POST);      

     add_slashes($_COOKIE);      

}      

      

function add_slashes($string) {      

     if (is_array($string)) {      

         foreach ($string as $key => $value) {      

             $string[$key] = add_slashes($value);      

         }      

     } else {      

         $string = addslashes($string);      

     }  

     return $string;      

但是还可以更进一步进行重新编码,解码,代码如下:

//编码 

function htmlencode($str) {       

      if(emptyempty($str)) return;  

      if($str=="") return $str;        

      $str=trim($str);  

      $str=str_replace("&","&",$str);  

      $str=str_replace(">",">",$str);  

      $str=str_replace("<","&lt;",$str);  

      $str=str_replace(chr(32),"&nbsp;",$str);  

      $str=str_replace(chr(9),"&nbsp;",$str);  

      $str=str_replace(chr(34),"&",$str);  

      $str=str_replace(chr(39),"&#39;",$str);  

      $str=str_replace(chr(13),"<br />",$str);  

      $str=str_replace("'","''",$str);  

      $str=str_replace("select","sel&#101;ct",$str);  

      $str=str_replace("join","jo&#105;n",$str);  

      $str=str_replace("union","un&#105;on",$str);  

      $str=str_replace("where","wh&#101;re",$str);  

      $str=str_replace("insert","ins&#101;rt",$str);  

      $str=str_replace("delete","del&#101;te",$str);  

      $str=str_replace("update","up&#100;ate",$str);  

      $str=str_replace("like","lik&#101;",$str);  

      $str=str_replace("drop","dro&#112;",$str);  

      $str=str_replace("create","cr&#101;ate",$str);  

      $str=str_replace("modify","mod&#105;fy",$str);  

      $str=str_replace("rename","ren&#097;me",$str);  

      $str=str_replace("alter","alt&#101;r",$str);  

      $str=str_replace("cast","ca&#115;",$str);        

      return $str;   

这样就能更放心的对外来数据进行入库处理了,但是从数据库取出来,在前台显示的时候,必须重新解码一下,代码如下:

//解码 

 

function htmldecode($str) {       

      if(emptyempty($str)) return;  

      if($str=="")  return $str;  

      $str=str_replace("sel&#101;ct","select",$str);  

      $str=str_replace("jo&#105;n","join",$str);  

      $str=str_replace("un&#105;on","union",$str);  

      $str=str_replace("wh&#101;re","where",$str);  

      $str=str_replace("ins&#101;rt","insert",$str);  

      $str=str_replace("del&#101;te","delete",$str);  

      $str=str_replace("up&#100;ate","update",$str);  

      $str=str_replace("lik&#101;","like",$str);  

      $str=str_replace("dro&#112;","drop",$str);  

      $str=str_replace("cr&#101;ate","create",$str);  

      $str=str_replace("mod&#105;fy","modify",$str);  

      $str=str_replace("ren&#097;me","rename",$str);  

      $str=str_replace("alt&#101;r","alter",$str);  

      $str=str_replace("ca&#115;","cast",$str);  

      $str=str_replace("&amp;","&",$str);  

      $str=str_replace("&gt;",">",$str);  

      $str=str_replace("&lt;","<",$str);  

      $str=str_replace("&nbsp;",chr(32),$str);  

      $str=str_replace("&nbsp;",chr(9),$str);  

      $str=str_replace("&",chr(34),$str);  

      $str=str_replace("&#39;",chr(39),$str);  

      $str=str_replace("<br />",chr(13),$str);  

      $str=str_replace("''","'",$str);   //开源代码phpfensi.com     

      return $str;  

虽然多了一步编码,解码的过程,但是安全方面,会更进一步,要如何做,自己取舍吧.

再附一些代码如下:

function safe_replace($string) { 

 $string = str_replace(' ','',$string); 

 $string = str_replace(''','',$string); 

 $string = str_replace(''','',$string); 

 $string = str_replace('*','',$string); 

 $string = str_replace('"','"',$string); 

 $string = str_replace("'",'',$string); 

 $string = str_replace('"','',$string); 

 $string = str_replace(';','',$string); 

 $string = str_replace('<','<',$string);

$string = str_replace('>','>',$string); 

$string = str_replace("{",'',$string);

$string = str_replace('}','',$string)

return $string ;

}

//더 포괄적인 코드는 다음과 같습니다.

//제출된 데이터 처리

function htmldecode($str) {

if (emptyempty ( $str ) || "" == $str) {

return ""; }

$str = Strip_tags( $str );

$str = htmlspecialchars( $str ); $str = nl2br( $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 ( "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 ( " 삭제" , "", $str );

$str = str_replace ( "업데이트", "", $str );

$str = str_replace ( "좋아요", "", $str )

$str = str_replace ( "drop", "", $str )

$str = str_replace ( "create", "", $str ); >

$str = str_replace ( "수정", "", $str )

$str = str_replace ( "이름 바꾸기", "", $str ); = str_replace ( "변경", "", $str );

$str = str_replace ( "캐스트", "", $str )

$farr = array ("//s+/", //과도한 공백 필터링

"/<(//?)(img|script|i?frame|style|html|body|title|link| Meta| /?|/%)([^>]*?)>/isU", //악성컨텐츠나 악성코드 유입을 방지하기 위해 필터<스크립트. 플래시 등을 삽입할 필요가 없는 경우 ., 또한 [a-zA-Z]+/s*=([^>]*>에 <객체 필터링

"/(<[^>]*)을 추가할 수도 있습니다. )/isU" )//이벤트 시 javascript 필터링

;

$tarr = array (" ", "", //안전하지 않은 태그를 직접 삭제하려면 그대로 두면 됩니다. 공백

"" ) ;

return $str

}

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!