《PHP學(xué)習(xí):Yii凈化器CHtmlPurifier用法示例(過濾不良代碼)》要點(diǎn):
本文介紹了PHP學(xué)習(xí):Yii凈化器CHtmlPurifier用法示例(過濾不良代碼),希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
PHP實(shí)戰(zhàn)本文實(shí)例講述了Yii凈化器CHtmlPurifier用法.分享給大家供大家參考,具體如下:
PHP實(shí)戰(zhàn)1. 在控制器中使用:
PHP實(shí)戰(zhàn)
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));
}
}
PHP實(shí)戰(zhàn)2. 在模型中的使用:
PHP實(shí)戰(zhàn)
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;
}
}
PHP實(shí)戰(zhàn)3. 在過濾器中的使用:
PHP實(shí)戰(zhàn)
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();
}
PHP實(shí)戰(zhàn)4. 在視圖中的使用:
PHP實(shí)戰(zhàn)
<?php $this->beginWidget('CHtmlPurifier'); ?>
...display user-entered content here...
<?php $this->endWidget(); ?>
PHP實(shí)戰(zhàn)更多關(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ù)庫操作技巧匯總》
PHP實(shí)戰(zhàn)希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/5656.html