wordpress屏蔽垃圾评论的方法
垃圾评论一般是由机器发布了这样可以导致几分钟内发上万千上万条垃圾信息了,这些对于我们来讲没有任何作用了,那么要如何来屏蔽这些垃圾评论呢?
大部分垃圾评论都是用自动化软件来发的,它会先GET一个页面,然后再往wp-comments-post.php文件POST内容.
1、使用插件
垃圾评论过滤:Akismet插件
wordpress评论滑动解锁:myQaptcha插件
发现效果不好,Akismet虽然能过滤垃圾评论但是阻止不了机器人提交评论,而且一来就是几百个连接,VPS负载直接飙到4点几,CPU占用率也很高,导致博客打开异常慢,上个月Akismet统计过滤了1,477,296条的垃圾评论怪不得会慢。
myQaptcha安装后没有起色,看日志里被绕过了。
2、直接屏蔽IP
网上找了一个类似功能修改下,先看下IP在日志里是第几个字段,第一个就填$1,第二个就填$2以此类推.
先从日志中过滤出IP,代码如下:
awk '/wp-comments-post/ { printf $1 " "}' / var /log/httpd/live- in .org-access_log* | sort | uniq -d -c | awk '$1 > 10 {printf $2 " "}' >/tmp/iptables.txt 2>/dev/ null结果保存到/tmp/iptables.txt中。
iptables屏蔽,代码如下:
awk '{system("iptables -I INPUT -s "$0" -j DROP")}' /tmp/iptables.txt service iptables save将/tmp/iptables.txt内的IP添加到iptables中并保存,查看添加的内容,代码如下:
iptables -n -L |less
说明: -n表示不做DNS解析
提示: 如果效果不好,直接屏蔽IP段,直接一个php函数,代码如下:
// 垃圾评论拦截 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); } } //www.phpfensi.com 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. " 来路: " . $referer. " IP: " . $IP. " 方式: " . $way. " ?热? " . $spamcom. " -- 记录成功 --" ; } } function sink( $comment ) { if ( !empty($_POST[ 'spam_confirmed' ]) ) { if ( in_array( $comment[ 'comment_type' ], array( 'pingback' , 'trackback' ) ) ) return $comment; //方法一: 直接挡掉, ? die(); 前面两斜线?h除即可. 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();查看更多关于wordpress屏蔽垃圾评论的方法 - WordPress的详细内容...