<sup id="0sgeu"><center id="0sgeu"></center></sup>
<rt id="0sgeu"><small id="0sgeu"></small></rt><rt id="0sgeu"><center id="0sgeu"></center></rt>
<acronym id="0sgeu"></acronym><rt id="0sgeu"></rt>

HTML Purifier簡介與安裝配置使用說明

更新時間:2021-08-04 編輯:未知 關注人次:0 云搜索


在PHP里解決XSS最簡單的方法是使用htmlspecialchars轉義xml實體,但對于需要使用xml的時候就搏手無策了。之前一直使用一個叫RemoveXSS的函數,該函數過濾得比較嚴格,很多html特性都過濾了,而且有bug,不修改代碼使用起來很不友好,修改了卻無法應對靈活的XSS攻擊。


HTML Purifier是基于php 5所編寫的HTML過濾器,支持自定義過濾規則,還可以把不標準的HTML轉換為標準的HTML,是WYSIWYG編輯器的福音。


HTML Purifier可以幫助用戶保障HTML的合法性,它可以使你確認HTML是否包含跨站腳本攻擊企圖或其它的惡意攻擊。通過該軟件你準許用戶粘貼一些HTML內容,卻不會讓其插入惡意代碼,而這種代碼可在查看這些HTML的任何人的瀏覽器中運行。我們可以通過CodeIgniter、 Drupal、 MODx、 Phorum、 Joomla! 、WordPress等來使用該軟件。


2、使用HTML Purifier的要求

php 5+

iconv

bcmath

tidy


3、安裝配置


3.1下載安裝

http://htmlpurifier.org/download

直接下載zip版,把解壓后的htmlpurifier中的library拷貝到項目中就可以了。


3.2初始化和設置


require_once ‘htmlpurifier-4.5.0/library/HTMLPurifier.auto.php’; //引用


$config = HTMLPurifier_Config::createDefault();

$config->set(‘HTML.AllowedElements’, array(‘div’=>true, ‘table’=>true, ‘tr’=>true, ‘td’=>true, ‘br’=>true)); //允許屬性 div table tr td br元素

$config->set(‘HTML.Doctype’, ‘XHTML 1.0 Transitional’) //html文檔類型

$config->set(‘Core.Encoding’, ‘UTF-8’) //字符編碼


4、調用并使用


$purifier = new HTMLPurifier($config); //new一個

$puri_html = $purifier->purify($html); //搞定~!


阿里云-推廣AD

5、看了其他攻略中,有一個新建一個類的使用方法,也不錯,粘過來


require_once ‘htmlpurifier-4.5.0/library/HTMLPurifier.auto.php’;


class Resume_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(!is_null($options)){

foreach($options as $option){

$config->set($option[0], $option[1], $option[2]);

}

}

$this->_htmlPurifier = new HTMLPurifier($config);

}


public function filter($value)

{

return $this->_htmlPurifier->purify($value);

}

}


使用:

$conf = array(

array(‘HTML.AllowedElements’,

array(

‘div’ => true,

‘table’ => true,

‘tr’ => true,

‘td’ => true,

‘br’ => true,

‘img’=> true

),

false), //允許屬性 div table tr td br img元素


/* 以下幾行注釋去掉,過濾將變得非常嚴格,通常情況無需使用: */

//    array(‘HTML.AllowedAttributes’, array(‘class’ => TRUE), false), //允許屬性 class

//    array(‘Attr.ForbiddenClasses’, array(‘resume_p’ => TRUE), false), //禁止classes如

//    array(‘AutoFormat.RemoveEmpty’, true, false), //去空格

//    array(‘AutoFormat.RemoveEmpty.RemoveNbsp’, true, false), //去nbsp

//    array(‘URI.Disable’, true, false),

);


$p = new Resume_HtmlPurifier($conf);

$puri_html = $p->filter($html);


6、XSS攻擊示范代碼


‘>



link


本文地址: http://www.slhfoods.com/a/xinwenguandian/jianzhanzhishi/2021/0804/32444.html ,轉載請注明出處。

服務支持

我們珍惜您每一次在線詢盤,有問必答,用專業的態度,貼心的服務。

讓您真正感受到我們的與眾不同!

狼客人成网