好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php sql防注入以及 html 过滤安全函数 - php函数

php sql防注入以及 html 过滤安全函数

方法一过滤html自定义函数,代码如下:

function  ihtmlspecialchars( $string ) {   if ( is_array ( $string )) {   foreach ( $string   as   $key  =>  $val ) {   $string [ $key ] = ihtmlspecialchars( $val );   }  //开源代码phpfensi测试数据   }  else  {   $string  = preg_replace( '/&amp;((#(d{3,5}|x[a-fa-f0-9]{4})|[a-za-z][a-z0-9]{2,5});)/' ,  '&\1' ,   str_replace ( array ( '&' ,  '"' ,  '<' ,  '>' ),  array ( '&amp;' ,  '&quot;' ,  '&lt;' ,  '&gt;' ),  $string ));   }   return   $string ;   

方法二,代码如下:

// $rptype = 0 表示仅替换 html标记    // $rptype = 1 表示替换 html标记同时去除连续空白字符    // $rptype = 2 表示替换 html标记同时去除所有空白字符    // $rptype = -1 表示仅替换 html危险的标记    function  htmlreplace( $str , $rptype =0)   {   $str  =  stripslashes ( $str );   if ( $rptype ==0)   {   $str  = htmlspecialchars( $str );   }   else   if ( $rptype ==1)   {   $str  = htmlspecialchars( $str );   $str  =  str_replace ( " " , ' ' , $str );   $str  =  ereg_replace ( "[rnt ]{1,}" , ' ' , $str );   }   else   if ( $rptype ==2)   {   $str  = htmlspecialchars( $str );   $str  =  str_replace ( " " , '' , $str );   $str  =  ereg_replace ( "[rnt ]" , '' , $str );   }   else    {   $str  =  ereg_replace ( "[rnt ]{1,}" , ' ' , $str );   $str  =  eregi_replace ( 'script' , 'script' , $str );   $str  =  eregi_replace ( "<[/]{0,1}(link|meta|ifr|fra)[^>]*>" , '' , $str );   }   return   addslashes ( $str );   } 

其它方法,php过滤不安全字符函数,代码如下:

function  uh( $str )  {       $farr  =  array (           "/s+/" , //过滤多余的空白            "/<(/?)(script|i?frame|style|html|body|title|link|meta|?|%)([^>]*?)>/isu" , //过滤 <script 等可能引入恶意内容或恶意改变显示布局的代码,如果不需要插入flash等,还可以加入<object的过滤            "/(<[^>]*)on[a-za-z]+s*=([^>]*>)/isu" , //过滤网页特效的on事件       );      $tarr  =  array (           " " ,           "<\1\2\3>" ,  //如果要直接清除不安全的标签,这里可以留空            "\1\2" ,     );       $str  = preg_replace( $farr , $tarr , $str );     return   $str ; 

查看更多关于php sql防注入以及 html 过滤安全函数 - php函数的详细内容...

  阅读:56次