上一次分析了几种网站敏感字的过滤方案
(见网站敏感字过滤分析

今天发个本人写的一个简单的过滤敏感字的类
这个类的主要作用是过滤掉一些敏感的字词,好在某些关键时期能够让你的站尽可能的保持安全
详细情况请看代码中的注释(文章尾部提供下载)




<?php
/**
* 数据对象
*
* @category   model
* @package    model
* @author     Bob Xia <xiamr@qq.com>
* @version    $Id: dirtyWords.model.php 259 2008-07-02 07:18:01Z bob $
*/

/**
*
*
* @category   model
* @package    model
*/
class model_dirtyWords {
  
   //数据对象,一般将敏感字保存在文件中即可,敏感字之间用|分隔
  static public $file_name = '../conf/forbid.photo.keyword.edit.php';
  /**
   * 读取文件内容
   *
   * @return string
   */
  static public function readFile(){
    $fp = fopen(self::$file_name,'r');
    $keywords = filesize(self::$file_name)?fread($fp,filesize(self::$file_name)):'';
    fclose($fp);
    return $keywords;
  }
  /**
   * 找出禁止出现的词汇
  *      找出第一个被禁止的关键字,并返回,如果没有敏感字,返回false,
  *      这个方法的好处是,返回不能出现的字给输入者,提醒输入者更换,并悄悄的将输入者发布的文章记入
           数据库,或是文件,提醒管理员此文章可能需要审核
           这样,输入者即使通过入增加空格的形式来发表不当言论,但一方面管理员能迅速找到文章,另外一方面,
           输入者的不当言论经过他自己的处理后,也更难被有关方面发现,给了管理员更多的审核时间

  * @param string $string
   * @return string
   */
  static public function findDirty($string){
    $keywords = self::readFile();
    $keyword = array();
    $keyword = explode('|',$keywords);
    foreach ($keyword as $temp)
    {
      $result  = stristr($string,$temp);
      if($result)
        return $temp;
    }
    return FALSE;
  }
  
  /**
   * 写文件
                   主要用于后台管理敏感字数据
  *
   * @param sting $string
   * @return bool
   */
  static public function writeFile($string){
    $fp = fopen(self::$file_name,"w");
    fwrite($fp,$string);
    fclose($fp);
    return true;
  }

  /**
   * 过滤禁止出现的词汇
                    比较主流的用法,不推荐,将关键字替换成*号,可能会严重影响用户体验
  *
   * @param string $string
   * @return string
   */
  static public function filtrate($string){
    $keywords = self::readFile();
    $string = preg_replace("/$keywords/i",'*',$string);
    return $string;
  }
}


这个类使用方法较为简单,大家可以自己揣摩,再结合我的网站敏感字过滤分析文章,希望对于初学者能有一定帮组

下载文件 (已下载 21 次)


请给这篇日志评个分吧!

88 分数: 9.0/10 (2 votes 票)

工作 » LAMP | 评论(0) | 引用(0) | 阅读(488)
发表评论
 网址
 电邮
  密码 游客无需密码
 昵称  *  [注册]
               

 
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我