在公共函數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!!