> php教程 > PHP开发 > 본문

Yii 정화기 CHtmlPurifier 사용 예(잘못된 코드 필터링)

高洛峰
풀어 주다: 2016-12-30 14:49:29
원래의
1105명이 탐색했습니다.

이 글의 예시에서는 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(&#39;CHtmlPurifier&#39;); ?>
...display user-entered content here...
<?php $this->endWidget(); ?>
로그인 후 복사

이 글이 Yii 프레임워크를 기반으로 하는 모든 사람의 PHP 프로그램 설계에 도움이 되기를 바랍니다.

Yii 정화기 CHtmlPurifier 사용 예(잘못된 코드 필터링) 관련 기사를 더 보려면 PHP 중국어 웹사이트를 주목하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!