php处理sql注入问题 如何防止双重转义

<?php

//php防注入和XSS攻击通用过滤. 

//by qq:831937

$_GET       && SafeFilter($_GET);

$_POST      && SafeFilter($_POST);

$_COOKIE    && SafeFilter($_COOKIE);

  

function SafeFilter (&$arr)

{  

      if (is_array($arr))

     {

          foreach ($arr as $key => $value)

          {

               if (!is_array($value))

               {

                    if (!get_magic_quotes_gpc())    //不对magic_quotes_gpc转义过的字符使用addslashes(),避免双重转义。

                    {

                         $value    = addslashes($value);    //给单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)加上反斜线转义

                    }

                    $arr[$key]         = htmlspecialchars($value,ENT_QUOTES);   //&,",',> ,< 转为html实体 &amp;,&quot;&#039;,&gt;,&lt;

               }

               else

               {

                    SafeFilter($arr[$key]);

               }

          }

     }

}

?>

猜你喜欢

转载自blog.csdn.net/tv002/article/details/87105862