Yii凈化器CHtmlPurifier用法示例(過濾不良代碼)
本文實(shí)例講述了Yii凈化器CHtmlPurifier用法。分享給大家供大家參考,具體如下:
1. 在控制器中使用:
public function actionCreate()
{
$model=new News;
$purifier = new CHtmlPurifier();
$purifier->options = array(
'URI.AllowedSchemes'=>array(
'http' => true,
'https' => true,
),
'HTML.Allowed'=>'div',
);
if(isset($_POST['News']))
{
$model->attributes=$_POST['News'];
$model->attributes['content'] = $purifier->purify($model->attributes['content']);
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
}
2. 在模型中的使用:
protected function beforeSave()
{
$purifier = new CHtmlPurifier();
$purifier->options = array(
'URI.AllowedSchemes'=>array(
'http' => true,
'https' => true,
),
'HTML.Allowed'=>'div',
);
if(parent::beforeSave()){
if($this->isNewRecord){
$this->create_data = date('y-m-d H:m:s');
$this->content = $purifier->purify($this->content);
}
return true;
}else{
return false;
}
}
3. 在過濾器中的使用:
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
'purifier + create', //載入插入頁面時(shí)進(jìn)行些過濾操作
);
}
public function filterPurifier($filterChain){
$purifier = new CHtmlPurifier();
$purifier->options = array(
'URI.AllowedSchemes'=>array(
'http' => true,
'https' => true,
),
'HTML.Allowed'=>'div',
);
if(isset($_POST['news']){
$_POST['news']['content'] = $purify($_POST['news']['content']);
}
$filterChain->run();
}
4. 在視圖中的使用:
<?php $this->beginWidget('CHtmlPurifier'); ?>
...display user-entered content here...
<?php $this->endWidget(); ?>
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- 詳解在PHP的Yii框架中使用行為Behaviors的方法
- YII框架行為behaviors用法示例
- YII2.0框架行為(Behavior)深入詳解
- Yii控制器中filter過濾器用法分析
- YII Framework的filter過濾器用法分析
- PHP的Yii框架中過濾器相關(guān)的使用總結(jié)
- 詳解PHP的Yii框架中的Controller控制器
- Yii2創(chuàng)建控制器(createController)方法詳解
- yii2控制器Controller Ajax操作示例
- Yii2設(shè)置默認(rèn)控制器的兩種方法
- yii2 在控制器中驗(yàn)證請求參數(shù)的使用方法
- Yii2.0框架behaviors方法使用實(shí)例分析
相關(guān)文章
在laravel框架中實(shí)現(xiàn)封裝公共方法全局調(diào)用
今天小編就為大家分享一篇在laravel框架中實(shí)現(xiàn)封裝公共方法全局調(diào)用,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
如何優(yōu)雅的使用 laravel 的 validator驗(yàn)證方法
web 開發(fā)過程中經(jīng)常會需要進(jìn)行參數(shù)驗(yàn)證,這篇文章主要介紹了如何優(yōu)雅的使用 laravel 的 validator驗(yàn)證方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11
一次因composer錯(cuò)誤使用引發(fā)的問題與解決
這篇文章主要給大家介紹了一次因composer錯(cuò)誤使用引發(fā)的問題與解決方法,文中通過示例代碼以及圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
PHP實(shí)現(xiàn)簡易用戶登錄系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)簡易用戶登錄系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
linux系統(tǒng)下php安裝mbstring擴(kuò)展的二種方法
這篇文章主要介紹了linux系統(tǒng)環(huán)境下,php安裝mbstring擴(kuò)展的二種方法,大家參考使用吧2014-01-01

