php如何预防sql注入漏洞

1705
2021/1/27 17:07:28
栏目: 云计算
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

php如何预防sql注入漏洞

php预防sql注入漏洞的方法:

利用magic_quotes_gpc指令或它的搭挡addslashes()函数进行过滤,例如:

<?php

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

$_GET     && SafeFilter($_GET);

$_POST    && SafeFilter($_POST);

$_COOKIE  && SafeFilter($_COOKIE);

  

function SafeFilter (&$arr) 

{

   $ra=Array('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/','/script/','/javascript/','/vbscript/','/expression/','/applet/'

   ,'/meta/','/xml/','/blink/','/link/','/style/','/embed/','/object/','/frame/','/layer/','/title/','/bgsound/'

   ,'/base/','/onload/','/onunload/','/onchange/','/onsubmit/','/onreset/','/onselect/','/onblur/','/onfocus/',

   '/onabort/','/onkeydown/','/onkeypress/','/onkeyup/','/onclick/','/ondblclick/','/onmousedown/','/onmousemove/'

   ,'/onmouseout/','/onmouseover/','/onmouseup/','/onunload/');

     

   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 字符)

             加上反斜线转义

          }

          $value   = preg_replace($ra,'',$value);     //删除非打印字符,粗暴式过滤xss可疑字符串

          $arr[$key] = htmlentities(strip_tags($value)); //去除HTML和PHP标记并转换为HTML实体

        }

        else

        {

          SafeFilter($arr[$key]);

        }

     }

   }

}

?>




辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: php中array函数的用法是什么