PHP常用工具函数小结【移除XSS攻击、UTF8与GBK编码转换等】


Posted in PHP onApril 27, 2019

本文实例总结了PHP常用工具函数。分享给大家供大家参考,具体如下:

移除XSS攻击脚本

function RemoveXSS($val) {
    // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
    // this prevents some character re-spacing such as <java\0script>
    // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
    $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
    // straight replacements, the user should never need these since they're normal characters
    // this prevents like <IMG SRC=@avascript:alert('XSS')>
    $search = 'abcdefghijklmnopqrstuvwxyz';
    $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $search .= '1234567890!@#$%^&*()';
    $search .= '~`";:?+/={}[]-_|\'\\';
    for ($i = 0; $i < strlen($search); $i++) {
      // ;? matches the ;, which is optional
      // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
      // @ @ search for the hex values
      $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
      // @ @ 0{0,7} matches '0' zero to seven times
      $val = preg_replace('/(�{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
    }
    // now the only remaining whitespace attacks are \t, \n, and \r
    $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
    $ra2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
    $ra = array_merge($ra1, $ra2);
    $found = true; // keep replacing as long as the previous round replaced something
    while ($found == true) {
      $val_before = $val;
      for ($i = 0; $i < sizeof($ra); $i++) {
        $pattern = '/';
        for ($j = 0; $j < strlen($ra[$i]); $j++) {
          if ($j > 0) {
            $pattern .= '(';
            $pattern .= '(&#[xX]0{0,8}([9ab]);)';
            $pattern .= '|';
            $pattern .= '|(�{0,8}([9|10|13]);)';
            $pattern .= ')*';
          }
          $pattern .= $ra[$i][$j];
        }
        $pattern .= '/i';
        $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
        $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
        if ($val_before == $val) {
          // no replacements were made, so exit the loop
          $found = false;
        }
      }
    }
    return $val;
}

GBK转UTF8

function GBKtoUTF8($str)
{
  if(is_array($str))
  {
    foreach ($str as &$value)
    {
      $value = GBKtoUTF8($value);
    }
    return $str;
  }elseif(is_string($str)){
    $str = iconv("GB18030", "UTF-8//IGNORE", $str);
    return $str;
  }else{
    return $str;
  }
}

UTF8转GBK

function UTF8toGBK(&$str)
{
  if(is_array($str))
  {
    foreach ($str as &$value)
    {
      $value = UTF8toGBK($value);
    }
    return $str;
  }elseif (is_string($str)){
    $str = iconv("UTF-8", "GB18030//IGNORE", $str);
    return $str;
  }else{
    return $str;
  }
}

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
解决了Ajax、MySQL 和 Zend Framework 的乱码问题
Mar 03 PHP
一个比较简单的PHP 分页分组类
Dec 10 PHP
用PHP读取和编写XML DOM的实现代码
Feb 03 PHP
PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
Oct 05 PHP
深入for,while,foreach遍历时间比较的详解
Jun 08 PHP
PHP 解决session死锁的方法
Jun 20 PHP
ci检测是ajax还是页面post提交数据的方法
Nov 10 PHP
ThinkPHP连接Oracle数据库
Apr 22 PHP
修改yii2.0用户登录使用的user表为其它的表实现方法(推荐)
Aug 01 PHP
PHP5中使用mysqli的prepare操作数据库的介绍
Mar 18 PHP
因str_replace导致的注入问题总结
Aug 08 PHP
tp5.1 框架数据库高级查询技巧实例总结
May 25 PHP
PHP操作路由器实现方法示例
Apr 27 #PHP
PHP切割汉字的常用方法实例总结
Apr 27 #PHP
YII框架常用技巧总结
Apr 27 #PHP
YII框架行为behaviors用法示例
Apr 26 #PHP
YII框架模块化处理操作示例
Apr 26 #PHP
Yii框架ACF(accessController)简单权限控制操作示例
Apr 26 #PHP
YII框架实现自定义第三方扩展操作示例
Apr 26 #PHP
You might like
PHP+MySQL 手工注入语句大全 推荐
2009/10/30 PHP
php模拟asp中的XmlHttpRequest实现http请求的代码
2011/03/24 PHP
三个类概括PHP的五种设计模式
2012/09/05 PHP
PHP 处理TXT文件(打开/关闭/检查/读取)
2013/05/13 PHP
Laravel 5框架学习之路由、控制器和视图简介
2015/04/07 PHP
CodeIgniter分页类pagination使用方法示例
2016/03/28 PHP
php-fpm开启状态统计的方法详解
2017/06/23 PHP
在Laravel5.6中使用Swoole的协程数据库查询
2018/06/15 PHP
深入理解PHP+Mysql分布式事务与解决方案
2020/12/03 PHP
Aster vs Newbee BO5 第二场2.19
2021/03/10 DOTA
jqPlot jquery的页面图表绘制工具
2009/07/25 Javascript
javascript cookie操作类的实现代码小结附使用方法
2010/06/02 Javascript
JS getAttribute和setAttribute(取得和设置属性)的使用介绍
2013/07/10 Javascript
固定网页背景图同时保持图片比例的思路代码
2013/08/15 Javascript
javascript实现简单的二级联动
2015/03/19 Javascript
jQuery层动画定位滑动效果的方法
2015/04/30 Javascript
js实现滚动条滚动到页面底部继续加载
2015/12/19 Javascript
jQuery 弹出层插件(推荐)
2016/05/24 Javascript
jquery.zclip轻量级复制失效问题
2017/01/08 Javascript
使用Bootstrap + Vue.js实现表格的动态展示、新增和删除功能
2017/11/27 Javascript
微信小程序之裁剪图片成圆形的实现代码
2018/10/11 Javascript
详解Vue前端对axios的封装和使用
2019/04/01 Javascript
记一次Vue.js混入mixin的使用(分权限管理页面)
2019/04/17 Javascript
vue实现短信验证码登录功能(流程详解)
2019/12/10 Javascript
Python常用的文件及文件路径、目录操作方法汇总介绍
2015/05/21 Python
Django 重写用户模型的实现
2019/07/29 Python
python支持多线程的爬虫实例
2019/12/21 Python
selenium自动化测试入门实战
2020/12/21 Python
pandas 按日期范围筛选数据的实现
2021/02/20 Python
介绍一下Linux内核的排队自旋锁
2014/01/04 面试题
会计电算化专业毕业生求职信范文
2013/12/10 职场文书
母亲追悼会答谢词
2014/01/27 职场文书
西式婚礼主持词
2014/03/13 职场文书
新春寄语大全
2014/04/09 职场文书
专家推荐信模板
2014/05/09 职场文书
工作推荐信范文
2014/05/10 职场文书