Yii凈化器CHtmlPurifier用法示例(過濾不良代碼)
本文實例講述了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', //載入插入頁面時進行些過濾操作 ); } 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(); ?>
更多關于Yii相關內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
相關文章
在laravel框架中實現(xiàn)封裝公共方法全局調(diào)用
今天小編就為大家分享一篇在laravel框架中實現(xiàn)封裝公共方法全局調(diào)用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10如何優(yōu)雅的使用 laravel 的 validator驗證方法
web 開發(fā)過程中經(jīng)常會需要進行參數(shù)驗證,這篇文章主要介紹了如何優(yōu)雅的使用 laravel 的 validator驗證方法,非常具有實用價值,需要的朋友可以參考下2018-11-11linux系統(tǒng)下php安裝mbstring擴展的二種方法
這篇文章主要介紹了linux系統(tǒng)環(huán)境下,php安裝mbstring擴展的二種方法,大家參考使用吧2014-01-01