zoukankan      html  css  js  c++  java
  • htmlpurifier 过滤危险的JS代码

       在公共函数function里面

          

    // 有选择性的过滤XSS --》 说明:性能非常低-》尽量少用
    function removeXSS($data)
    {
    require_once './HtmlPurifier/HTMLPurifier.auto.php';
    $_clean_xss_config = HTMLPurifier_Config::createDefault();
    $_clean_xss_config->set('Core.Encoding', 'UTF-8');
    // 设置保留的标签
    $_clean_xss_config->set('HTML.Allowed','div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]');
    $_clean_xss_config->set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align');
    $_clean_xss_config->set('HTML.TargetBlank', TRUE);
    $_clean_xss_obj = new HTMLPurifier($_clean_xss_config);
    // 执行过滤
    return $_clean_xss_obj->purify($data);
    }

    在模型中调用这个方法

    protected function _before_insert(&$data,$option)
    {
    //获取当前时间添加到表单中
    $data['addtime']=date('Y-m-d H:i:s',time());

    //我们自己来过滤这个字段
    $data['goods_desc']= removeXSS($_POST['goods_desc']);
    }

    如果项目中使用了在线编辑器需要配合使用HTMLPurifer实现有选择性的过滤XSS!!

    世上无难事,只怕有心人......
  • 相关阅读:
    php 解析xml
    php
    php 设置自动加载某个页面
    Mac
    mysql
    Git
    C#
    C# 正则表达式
    C# ASCII码排序
    (转)datagridview 自定义列三步走
  • 原文地址:https://www.cnblogs.com/gooderic/p/5679300.html
Copyright © 2011-2022 走看看