WP酷 WP酷
  • 主题
  • 教程
  • 插件
  • 官方 QQ 群
  • 建站推荐
  • 联系
  • nicetheme® 奈思主题
  • 注册
    登录
立即登录
  • 请到 [后台->外观->菜单] 中设置菜单
首页 › WordPress 教程 › 可否与垃圾评论说再见?Willin Kan之Anti-Spam版本集合

可否与垃圾评论说再见?Willin Kan之Anti-Spam版本集合

PCDotFan10年前

可否与垃圾评论说再见?Willin Kan之Anti-Spam版本集合-WP酷

 

从前有一种肉罐头,它叫SPAM;现在有一种Junk评论,也叫SPAM。垃圾评论一直是我们所唾弃的东西,许多的博主为了提高自己的网站权重,不择手段地去各个网站发布自己的链接:

可否与垃圾评论说再见?Willin Kan之Anti-Spam版本集合-WP酷而这种发垃圾评论的人就被称之为spammer,Willin Kan大师为WordPress制作了一款极有力的代码防垃圾评论——小墙(Anti Spam)今日WP酷正想收集,也希望能有更多的人将此代码完善。所有版本代码均添加到主题文件functions.php的<?php 后。

 

Anti-Spam v1.90最新版

/* -----------------------------------------------
 <;<小牆>> Anti-Spam v1.9 by Willin Kan.
 */
 //建立
 class anti_spam {
   function anti_spam() {
     if ( !is_user_logged_in() ) {
       add_action('template_redirect', array($this, 'w_tb'), 1);
       add_action('pre_comment_on_post', array($this, 'gate'), 1);
       add_action('preprocess_comment', array($this, 'sink'), 1);
     }
   }
   //設欄位
   function w_tb() {
     if ( is_singular() ) {
       ob_start(create_function('$input', 'return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
    "textarea$1name=$2w$3$4/textarea><textarea name=\"comment\" cols=\"60\" rows=\"4\" style=\"display:none\"></textarea>", $input);') );
      }
   }
   //檢查
   function gate() {
     ( !empty($_POST['w']) && empty($_POST['comment']) ) ? $_POST['comment'] = $_POST['w'] : $_POST['spam_confirmed'] = 1;
   }
   //處理
   function sink( $comment ) {
     if ( !empty($_POST['spam_confirmed']) ) {
       //方法一:直接擋掉, 將 die(); 前面兩斜線刪除即可.
       //die();
       //方法二:標記為spam, 留在資料庫檢查是否誤判.
       add_filter('pre_comment_approved', create_function('', 'return "spam";'));
       $comment['comment_content'] = "[ 小牆判斷這是Spam! ]\n" . $comment['comment_content'];
     }
     return $comment;
   } 
}
$anti_spam = new anti_spam();
// -- END ----------------------------------------

 

 

Anti-Spam v1.84

//Spam
/* <<小牆>> Anti-Spam v1.84 by Willin Kan. */
class anti_spam {
  function anti_spam() {
    if ( !current_user_can('read') ) {
      add_action('template_redirect', array($this, 'w_tb'), 1);
      add_action('init', array($this, 'gate'), 1);
      add_action('preprocess_comment', array($this, 'sink'), 1);
    }
  }
  // 設欄位
  function w_tb() {
    if ( is_singular() ) {
      // 非中文語系
      if ( stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'], 'zh') === false ) {
        add_filter( 'comments_open', create_function('', "return false;") ); // 關閉評論
      } else {
        ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
        "textarea$1name=$2w$3$4/textarea>",$input);') ); } } } // 檢查 function gate() { $w = 'w'; if ( !empty($_POST[$w]) && empty($_POST['comment']) ) { $_POST['comment'] = $_POST[$w]; } else { $request = $_SERVER['REQUEST_URI']; $way = isset($_POST[$w]) ? '手動操作' : '未經評論表格'; $spamcom = isset($_POST['comment']) ? $_POST['comment'] : ''; $_POST['spam_confirmed'] = "請求: ". $request. "\n方式: ". $way. "\n內容: ". $spamcom. "\n -- 記錄成功 --"; } } // 處理 function sink( $comment ) { // 不管 Trackbacks/Pingbacks if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment; // 已確定為 spam if ( !empty($_POST['spam_confirmed']) ) { // 方法一: 直接擋掉, 將 die(); 前面兩斜線刪除即可. //die(); // 方法二: 標記為 spam, 留在資料庫檢查是否誤判. add_filter('pre_comment_approved', create_function('', 'return "spam";')); $comment['comment_content'] = "[ 小牆判斷這是Spam! ]\n". $_POST['spam_confirmed']; $this->add_black( $comment ); } else { // 檢查頭像 $f = md5( strtolower($comment['comment_author_email']) ); $g = sprintf( "http://%d.gravatar.com", (hexdec($f{0}) % 2) ) .'/avatar/'. $f .'?d=404'; $headers = @get_headers( $g ); if ( !preg_match("|200|", $headers[0]) ) { // 沒頭像的列入待審 add_filter('pre_comment_approved', create_function('', 'return "0";')); //$this->add_black( $comment ); } } return $comment; } // 列入黑名單 function add_black( $comment ) { if (!($comment_author_url = $comment['comment_author_url'])) return; if ($pos = strpos($comment_author_url, '//')) $comment_author_url = substr($comment_author_url, $pos + 2); if ($pos = strpos($comment_author_url, '/')) $comment_author_url = substr($comment_author_url, 0, $pos); $comment_author_url = strtr($comment_author_url, array('www.' => '')); if (!wp_blacklist_check('', '', $comment_author_url, '', '', '')) update_option('blacklist_keys', $comment_author_url . "\n" . get_option('blacklist_keys')); } } $anti_spam = new anti_spam();

 

Anti-Spam v1.83

/* <<小牆>> Anti-Spam v1.83 by Willin Kan. */
       class anti_spam {
       function anti_spam() {
       if ( !current_user_can('read') ) {
             add_action('template_redirect', array($this, 'w_tb'), 1);
             add_action('init', array($this, 'gate'), 1);
             add_action('preprocess_comment', array($this, 'sink'), 1);
       }
       }
       // 設欄位
       function w_tb() {
       if ( is_singular() ) {
       // 非中文語系
       if ( stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'], 'zh') === false ) {
               add_filter( 'comments_open', create_function('', "return false;") ); // 關閉評論
       } else {
       ob_start(create_function
       ('$input','return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
       "textarea$1name=$2w$3$4/textarea>",$input);') ); } } } // 檢查 function gate() { $w = 'w'; if ( !empty($_POST[$w]) && empty($_POST['comment']) ) { $_POST['comment'] = $_POST[$w]; } else { $request = $_SERVER['REQUEST_URI']; $IP = $_SERVER['REMOTE_ADDR']; // 可用於屏蔽 IP $way = isset($_POST[$w]) ? '手動操作' : '未經評論表格'; $spamcom = isset($_POST['comment']) ? $_POST['comment'] : ''; $_POST['spam_confirmed'] = "請求: ". $request. "\nIP: ". $IP. "\n方式: ". $way. "\n內容: ". $spamcom. "\n -- 記錄成功 --"; } } // 處理 function sink( $comment ) { // 不管 Trackbacks/Pingbacks if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment; // 已確定為 spam if ( !empty($_POST['spam_confirmed']) ) { // 方法一: 直接擋掉, 將 die(); 前面兩斜線刪除即可. //die(); // 方法二: 標記為 spam, 留在資料庫檢查是否誤判. add_filter('pre_comment_approved', create_function('', 'return "spam";')); $comment['comment_content'] = "[ 小牆判斷這是Spam! ]\n". $_POST['spam_confirmed']; $this->add_black( $comment ); } else { // 檢查頭像 $f = md5( strtolower($comment['comment_author_email']) ); $g = sprintf( "http://%d.gravatar.com", (hexdec($f{0}) % 2) ) .'/avatar/'. $f .'?d=404'; $headers = @get_headers( $g ); if ( !preg_match("|200|", $headers[0]) ) { // 沒頭像的列入待審 //、、注意!我暂时取消无头像列入待审 add_filter('pre_comment_approved', create_function('', 'return "0";')); //$this->add_black( $comment ); } } return $comment; } // 列入黑名單 function add_black( $comment ) { $blacklist = get_option('blacklist_keys'); update_option('blacklist_keys', $comment['comment_author'] . "\n" . $blacklist); } } $anti_spam = new anti_spam(); // -- END ----------------------------------------

 

Anti-Spam v1.82

/* <<小牆>> Anti-Spam v1.82 by Willin Kan. 2010/12/16 最新修改 */
//建立
class anti_spam {
  function anti_spam() {
    if ( !current_user_can('level_0') ) {
      add_action('template_redirect', array($this, 'w_tb'), 1);
      add_action('init', array($this, 'gate'), 1);
      add_action('preprocess_comment', array($this, 'sink'), 1);
    }
  }
  //設欄位
  function w_tb() {
    if ( is_singular() ) {
      ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
      "textarea$1name=$2w$3$4/textarea>",$input);') ); } } //檢查 function gate() { if ( !empty($_POST['w']) && empty($_POST['comment']) ) { $_POST['comment'] = $_POST['w']; } else { $request = $_SERVER['REQUEST_URI']; $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '隱瞞'; $IP = isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] . ' (透過代理)' : $_SERVER["REMOTE_ADDR"]; $way = isset($_POST['w']) ? '手動操作' : '未經評論表格'; $spamcom = isset($_POST['comment']) ? $_POST['comment'] : null; $_POST['spam_confirmed'] = "請求: ". $request. "\n來路: ". $referer. "\nIP: ". $IP. "\n方式: ". $way. "\n內容: ". $spamcom. "\n -- 記錄成功 --"; } } //處理 function sink( $comment ) { if ( !empty($_POST['spam_confirmed']) ) { if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment; //不管 Trackbacks/Pingbacks //方法一: 直接擋掉, 將 die(); 前面兩斜線刪除即可. //die(); //方法二: 標記為 spam, 留在資料庫檢查是否誤判. add_filter('pre_comment_approved', create_function('', 'return "spam";')); $comment['comment_content'] = "[ 小牆判斷這是Spam! ]\n". $_POST['spam_confirmed']; } return $comment; } } $anti_spam = new anti_spam(); // -- END ----------------------------------------

 

Anti-Spam v1.81

/* <<小墙>> Anti-Spam v1.81 by Willin Kan. */
//建立
class anti_spam {
  function anti_spam() {
    if ( !current_user_can('level_0') ) {
      add_action('template_redirect', array($this, 'w_tb'), 1);
      add_action('init', array($this, 'gate'), 1);
      add_action('preprocess_comment', array($this, 'sink'), 1);
    }
  }
  //设栏位
  function w_tb() {
    if ( is_singular() ) {
      ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=(["'])comment(["'])(.+)/textarea>#",
      "textarea$1name=$2w$3$4/textarea>",$input);') );
    }
  }
  //检查
  function gate() {
    if ( !empty($_POST['w']) && empty($_POST['comment']) ) {
      $_POST['comment'] = $_POST['w'];
    } else {
      $request = $_SERVER['REQUEST_URI'];
      $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER']         : '隐瞒';
      $IP      = isset($_SERVER["HTTP_VIA"])     ? $_SERVER["HTTP_X_FORWARDED_FOR"]. ' (透过代理)' : $_SERVER["REMOTE_ADDR"];
      $way     = isset($_POST['w'])              ? '手动操作'                       : '未经评论表格';
      $spamcom = isset($_POST['comment'])        ? $_POST['comment']                : null;
      $_POST['spam_confirmed'] = "请求: ". $request. "
来路: ". $referer. "
IP: ". $IP. "
方式: ". $way. "
内容: ". $spamcom. "
 -- 记录成功 --";
    }
  }
  //处理
  function sink( $comment ) {
    if ( !empty($_POST['spam_confirmed']) ) {
      if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment; //不管 Trackbacks/Pingbacks
      //方法一: 直接挡掉, 将 die(); 前面两斜线删除即可.
      //die();
      //方法二: 标记为 spam, 留在资料库检查是否误判.
      add_filter('pre_comment_approved', create_function('', 'return "spam";'));
      $comment['comment_content'] = "[ 小墙判断这是Spam! ]
". $_POST['spam_confirmed'];
    }
    return $comment;
  }
}
$anti_spam = new anti_spam();
// -- END ---------------------------------------- 

 

Anti-Spam v1.8

/* -----------------------------------------------
<<小牆>> Anti-Spam v1.8 by Willin Kan.
*/
//建立
class anti_spam {
function anti_spam() {
if ( !current_user_can('level_0') ) {
add_action('template_redirect', array($this, 'w_tb'), 1);
add_action('init', array($this, 'gate'), 1);
add_action('preprocess_comment', array($this, 'sink'), 1);
} }
//設欄位
function w_tb() {
if ( is_singular() ) {
ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
"textarea$1name=$2w$3$4/textarea>",$input);') ); } } //檢查 function gate() { ( !empty($_POST['w']) && empty($_POST['comment']) ) ? $_POST['comment'] = $_POST['w'] : $_POST['spam_confirmed'] = 1; } //處理 function sink( $comment ) { if ( !empty($_POST['spam_confirmed']) ) { //方法一:直接擋掉, 將 die(); 前面兩斜線刪除即可. //die(); //方法二:標記為spam, 留在資料庫檢查是否誤判. add_filter('pre_comment_approved', create_function('','return "spam";')); $is_ping = in_array( $comment['comment_type'], array('pingback', 'trackback') ); $comment['comment_content'] = ( $is_ping ) ? "◎ 這是 Pingback/Trackback, 小牆懷疑這可能是 Spam!\n" . $comment['comment_content'] : "[ 小牆判斷這是Spam! ]\n" . $comment['comment_content']; } return $comment; } } $anti_spam = new anti_spam(); // -- END ----------------------------------------

 

Anti-Spam v1.7

/* -----------------------------------------------
<<小牆>> Anti-Spam v1.7 by Willin Kan.
*/
//建立
class anti_spam {
function anti_spam() {
if (!current_user_can('level_0')) {
add_action('template_redirect',array($this,'w_tb'),1);
add_action('init',array($this,'gate'),1);
add_action('preprocess_comment',array($this,'sink'),1); } }
//設欄位
function w_tb() {
if (is_singular()) {
ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)#","textarea$1name=$2w$3$4
",$input);')); } } //檢查 function gate() { if (isset($_POST['w']) && !empty($_POST['w']) && empty($_POST['comment'])) { $_POST['comment']= $_POST['w'];unset($_POST['w']); } else {$_POST['spam_confirmed'] = 1;} } //處理 function sink($comment) { if (isset($_POST['spam_confirmed']) && !empty($_POST['spam_confirmed'])) { //方法一:直接擋掉, 將 die(); 前面兩斜線刪除即可. //die(); //方法二:標記為spam, 留在資料庫檢查是否誤判. add_filter('pre_comment_approved',create_function('','return "spam";')); $is_ping = in_array($comment['comment_type'], array('pingback', 'trackback')); if ($is_ping) { $comment['comment_content'] = "◎ 這是 Pingback/Trackback, 小牆懷疑這可能是 Spam!\n" .$comment['comment_content']; } else { $comment['comment_content'] = "[ 小牆判斷這是Spam! ]\n" .$comment['comment_content']; }} return $comment; }} $anti_spam = new anti_spam(); // -- END ----------------------------------------

 

#404#akismet#Anti#anti spam#bbbbbb#functions.php#gravatar#Ping#SPAM#willin#WordPress#垃圾评论怎么办#阻挡垃圾评论
1
分享

本站下载管理系统「dl.mywpku.com」因年久失修而永久关闭。若需获得往年主题,请至 WP酷的百度网盘(提取码: 5rbm) 自行搜索下载。默认解压密码为「www.mywpku.com」。

对您造成的不便,敬请谅解!

PCDotFan 站长
文章 435评论 859
相关文章
  • [6.0 更新中文汉化]极度流行的SEO插件 – WordPress SEO by Yoast
  • WordPress 新用户注册邮件链接提示「您的密码重设链接无效」
  • AMP – 为 WordPress 移动站点操作全球访问优化
  • 去除谷歌服务 – WordPress 国外主题全方位加速
  • 使用 WP-CLI 简化 WordPress 操作流程
  • WordPress 基础开发 – 一探 WordPress Hook 机制及示例应用
  • 优化静态资源 – WordPress 国外主题全方位加速
  • WordPress 实现微信登录
评论 (2)
再想想
  • Normal

    这段代码应该放在什么地方啊?

    10年前
    • WP酷

      @Normal 放在主题目录的functions.php下,抱歉我回复晚了……

      10年前
PCDotFan
站长
若无特别注明,则默认我发布的所有文章均为内容原创 / 翻译原创,转载时请保留来源。
435文章
859评论
3K获赞
猜你喜欢
简单实现首页幻灯片代码【Gztro版】
10年前
WordPress 低版本 IE 浏览器自动使用其它主题
9年前
jQuery实现侧边栏随滚动条滚动并固定位置
9年前
  • 杂记
  • WordPress 付费主题推荐
Copyright © 2023 WP酷. Designed by nicetheme. 琼ICP备13002067号-1