对富文本xss检查过滤比较好的开源项目的使用:htmlpurify,在codeigniter的应用

直接贴代码,能够更好的说明使用:

1,把下载包放到third_party中。

2,在library中创建适合ci使用的类库,代码如下:

<?php
require APPPATH.'third_party/htmlpurifier/HTMLPurifier.auto.php';//引入放到third_party中的类库文件
class Htmlfilter extends HTMLPurifier{  
    protected $_htmlPurifier = null;
    
    public function __construct($options = null){  
        $config = HTMLPurifier_Config::createDefault();  
        $config->set('Core.Encoding', 'UTF-8');    
        $config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
        if(!empty($options)){  
            foreach($options as $option){  
                $config->set($option[0], $option[1]);  
            }  
        }  
        $this->_htmlPurifier = new HTMLPurifier($config);  
    }
    
    public function filter($value){  
        return $this->_htmlPurifier->purify($value);  
    }  
}  
?>


3,在控制器中的使用,代码如下:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
    public function test(){
        $conf = array(  
            array('HTML.AllowedElements',  
                        array(  
                                'div' => true,  
                                'table' => true,  
                                'tr' => true,  
                                'td' => true,  
                                'br' => true,
                                'p' => true,
                            )), //允许属性 div table tr td br p元素  
                 array('HTML.AllowedAttributes', array('class' => TRUE,'style'=>TRUE)),//允许属性 class  及style
                 array('Attr.ForbiddenClasses', array('resume_p' => TRUE)),//禁止classes如  
                 array('AutoFormat.RemoveEmpty', true),//去空格  
                 array('AutoFormat.RemoveEmpty.RemoveNbsp', true),//去nbsp  
                 array('URI.Disable', true),  
        );
        $this->load->library('Htmlfilter',$conf);
        $html="<p style='color:red;'>hello world 1+1<3</p><script>alert(123)</script><style>color:red;</style>";
        $html = $this->htmlfilter->filter($html);
        echo $html;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值