WordPress无插件反垃圾评论:小墙

Jonty
2014-11-15 / 5 评论 / 52 阅读 / 正在检测是否收录...

小墙原理和功能

其原理可以简单理解为:添加一个隐藏的资料框(属性为 display:none 的 textarea),因为用户是看不到的,如果该区域被填满则判断为 Spam。

另外,小墙还在后台提供了spam 的基本资料,包含请求( REQUEST_URI )、来路( HTTP_REFERER )、IP、操作方式,,可提供下一步加强防护的参考。

使用 WP Anti Spam 小墙的方法

效果出色,免插件,将以下代码复制到主题的 functions.php 中即可

//Anti-Spam 防止垃圾评论
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 (strpos($comment_author_url, '//')) $comment_author_url = substr($comment_author_url, strpos($comment_author_url, '//') + 2);
if (strpos($comment_author_url, '/')) $comment_author_url = substr($comment_author_url, 0, strpos($comment_author_url, '/'));
update_option('blacklist_keys', $comment_author_url . "\n" . get_option('blacklist_keys'));
}
}
$anti_spam = new anti_spam();

```

本文共 224 个字数,平均阅读时长 ≈ 1分钟
0

打赏

海报

正在生成.....

评论 (5)

取消
  1. 头像
    World Lv.1
    中国江苏省 ·Windows 7 · Google Chrome
    沙发

    这个不错 哈哈

    回复 删除 垃圾
    1. 头像
      gkroot Lv.6
      中国河南省 ·iPhone · UC Browser
      @ World

      @World 还可以吧~

      回复 删除 垃圾
  2. 头像
    神情话意 Lv.3
    中国陕西省 ·Windows 8.1 · FireFox
    板凳

    嗯,这插件不错,以前用过一段时间,最近换成了自带的那个...主机国内的话可以修改下,给访问服务器那里加个socks5代理来解决被强的问题

    回复 删除 垃圾
    1. 头像
      gkroot Lv.6
      中国陕西省 ·Windows XP · Google Chrome
      @ 神情话意

      @神情话意

      :grin: :grin: 好羞射,说的真的好赞啊,顶你啊!
      回复 删除 垃圾
  3. 头像
    松果 Lv.2
    中国陕西省 ·Windows 7 · Google Chrome
    地毯

    小强 :x

    回复 删除 垃圾