相信很多wordpress使用者都有相同的经历,那就是在博客初期,可能不会有一些莫名的文章评论,而且写的内容貌似还比较符合文章的主题。但是,随着网站内容的丰富,相信会得到更多的spam评论。这些一般都是由一些机器人发出的,也不排除一些别有用心的人打算往你的博客里面注入点什么东东来获取点数据。所以屏蔽这些spam垃圾还是很有必要的。一般采取插件和非插件的方式:
一采用wordpress官方自带的防spam 插件:Akismet
WordPress自带的spam过滤插件Akismet,非常强大,可以过滤掉大部分垃圾评论。Akismet会对评论者、评论内容的关键字、评论者邮箱、链接地址做判断,从而确定是否要将评论列为待审批对象。虽然功能强大,但是也存在误判的情况,但是它最大的优点是“智能学习”。也就是当出现误判的时候,只要博主在后台将这条评论回复成正常即可,那么Akismet下次就不会过滤了。
二采用非插件方法:Anti-Spam
这个是由Willin Kan写的小墙工具,理论上可以100% 屏蔽机器人发出的spam;而如果是自然人提交评论,小墙会在评论提交表单中加一个hidden变量, 如果后台检测不到这个变量,则认定为spam,可以选择需要审核, 也可以直接过滤掉。在你的主题下面的functions.php里面添加如下代码即可

index.php
// 单独使用禁止全英文评论代码
function scp_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u'; //验证是否为中文
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "You should type some Chinese word (like \"你好\") in your comment to pass the spam-check, thanks for your patience! 您的评论中必须包含中文!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'scp_comment_post');
//综合使用< <小牆>> 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><textarea name=\"comment\" cols=\"100%\" rows=\"4\" style=\"display:none\">/textarea>",$input);') );
}
}
}
// 檢查
function gate() {
$w = 'w';
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'] : '';
$_POST['spam_confirmed'] = "請求: ". $request. "\n來路: ". $referer. "\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 ) {
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();最后记得把代码$2w$3$4中间的w和$w = ‘w’的w改成其它英文字母(但是二者要一致),比如$2wc$3和$w = ‘wc’等等,千万不要让spam发现额^^
评论
评论加载中…