Yii净化器CHtmlPurifier用法示例(过滤不良代码)
来源: 阅读:858 次 日期:2016-08-13 13:29:02
温馨提示: 小编为您整理了“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. 在视图中的使用:

beginWidget('CHtmlPurifier'); ?>

...display user-entered content here...

endWidget(); ?>

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

更多信息请查看 网络编程
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们| 联系我们| 人才招聘| 网站声明| 网站帮助| 非正式的简要咨询| 简要咨询须知| 加入群交流| 手机站点| 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00) 版权所有:
云南网警报警专用图标
Baidu
map