zoukankan      html  css  js  c++  java
  • 防止xss,sql攻击函数

    <?php
    //php防注入和XSS攻击通用过滤. 
    //by qq:831937
    $_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]);
            }
         }
       }
    }
    ?>
    
  • 相关阅读:
    AI编辑SVG格式的相关问题
    HTML里的id等属性命名需要注意
    canvas绘图动画细节
    触控获取坐标判断滑动方向
    CSS3卡片旋转效果
    使用CURL下载远程文件保存到服务器
    微信JS-SDK应用DEMO
    布局转换:文档流->绝对定位
    ThinkPHP缓存微信公众号access_token
    JAVA JSP笔记
  • 原文地址:https://www.cnblogs.com/ylcms/p/7275019.html
Copyright © 2011-2022 走看看