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!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM