yii如何过滤不良代码?本文主要介绍了yii净化器chtmlpurifier用法,可实现过滤不良代码的功能,涉及在控制器、模型、过滤器及视图中的相关使用技巧,需要的朋友可以参考下,希望对大家有所帮助。
具体如下:
1. 在控制器中使用:
public function actioncreate()
{
  $model=new news;
  $purifier = new chtmlpurifier();
  $purifier->options = array(
    'uri.allowedschemes'=>array(
              'http' => true,
              'https' => true,
    ),
       'html.allowed'=>'p',
  );
  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'=>'p',
  );
  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'=>'p',
  );
  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(); ?>
相关推荐:
yii2 modal弹窗之activeform实现ajax表单异步验证
yii2实现qq互联登录
yii2使用缓存的简单解析
以上就是yii如何过滤不良代码的详细内容。
   
 
   