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 相关文章推荐
PHP has encountered an Access Violation
Jan 15 PHP
php中的一个中文字符串截取函数
Feb 14 PHP
深入掌握include_once与require_once的区别
Jun 17 PHP
php生成缩略图示例代码分享(使用gd库实现)
Jan 20 PHP
PHP网络操作函数汇总
May 18 PHP
php+jQuery+Ajax实现点赞效果的方法(附源码下载)
Jul 21 PHP
php 无限级分类 获取顶级分类ID
Mar 13 PHP
PHP简单实现文本计数器的方法
Apr 28 PHP
php实现支付宝当面付(扫码支付)功能
May 30 PHP
phpMyAdmin通过密码漏洞留后门文件
Nov 20 PHP
PHP单例模式模拟Java Bean实现方法示例
Dec 07 PHP
php文件操作之文件写入字符串、数组的方法分析
Apr 15 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
MySQL连接数超过限制的解决方法
2011/07/17 PHP
php中的四舍五入函数代码(floor函数、ceil函数、round与intval)
2014/07/14 PHP
PHP计算日期相差天数实例分析
2016/02/23 PHP
PHP中类的继承和用法实例分析
2016/05/24 PHP
PHP在linux上执行外部命令的方法
2017/02/06 PHP
PHP生成zip压缩包的常用方法示例
2019/08/22 PHP
javascript开发技术大全-第3章 js数据类型
2011/07/03 Javascript
js实现addClass,removeClass,hasClass的函数代码
2011/07/13 Javascript
写自已的js类库需要的核心代码
2012/07/16 Javascript
Jquery 点击按钮自动高亮实现原理及代码
2014/04/25 Javascript
js实现select组件的选择输入过滤代码
2014/10/14 Javascript
js实现tab选项卡切换功能
2017/01/13 Javascript
js模态对话框使用方法详解
2017/02/16 Javascript
搭建简单的nodejs http服务器详解
2017/03/09 NodeJs
nodejs处理图片的中间件node-images详解
2017/05/08 NodeJs
简单实现js拖拽效果
2017/07/25 Javascript
vue中axios解决跨域问题和拦截器的使用方法
2018/03/07 Javascript
vue实现多个echarts根据屏幕大小变化而变化实例
2020/07/19 Javascript
[02:42]DOTA2城市挑战赛收官在即 四强之争风起云涌
2018/06/05 DOTA
Python面向对象基础入门之编码细节与注意事项
2018/12/11 Python
对pandas写入读取h5文件的方法详解
2018/12/28 Python
用sleep间隔进行python反爬虫的实例讲解
2020/11/30 Python
利用css3-animation实现逐帧动画效果
2016/03/10 HTML / CSS
科茨沃尔德家居商店:Scotts of Stow
2018/06/29 全球购物
澳大利亚优质的家居用品和生活方式公司:Bed Bath N’ Table
2019/04/16 全球购物
中国央视网签名寄语
2014/01/18 职场文书
技能比赛获奖感言
2014/02/14 职场文书
本科毕业生求职自荐信
2014/04/09 职场文书
企业文化演讲稿
2014/05/20 职场文书
安全生产知识竞赛活动总结
2014/07/07 职场文书
劳模先进事迹材料
2014/12/24 职场文书
会计专业求职信范文
2015/03/19 职场文书
宝宝满月祝酒词
2015/08/10 职场文书
MySQL复制问题的三个参数分析
2021/04/07 MySQL
HTML通过表单实现酒店筛选功能
2021/05/18 HTML / CSS
MySQL 四种连接和多表查询详解
2021/07/16 MySQL