《PHP學習:Yii凈化器CHtmlPurifier用法示例(過濾不良代碼)》要點:
本文介紹了PHP學習:Yii凈化器CHtmlPurifier用法示例(過濾不良代碼),希望對您有用。如果有疑問,可以聯系我們。
相關主題:YII框架
PHP實戰本文實例講述了Yii凈化器CHtmlPurifier用法.分享給大家供大家參考,具體如下:
PHP實戰1. 在控制器中使用:
PHP實戰
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實戰2. 在模型中的使用:
PHP實戰
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實戰3. 在過濾器中的使用:
PHP實戰
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
'purifier + create', //載入插入頁面時進行些過濾操作
);
}
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實戰4. 在視圖中的使用:
PHP實戰
<?php $this->beginWidget('CHtmlPurifier'); ?>
...display user-entered content here...
<?php $this->endWidget(); ?>
PHP實戰更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優秀開發框架總結》、《smarty模板入門基礎教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP實戰希望本文所述對大家基于Yii框架的PHP程序設計有所幫助.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/5656.html