Home > php教程 > php手册 > body text

直接拒绝wordpress黑名单评论的发表

WBOY
Release: 2016-06-06 20:09:11
Original
1031 people have browsed it

其实伦家不想这么快就发新文章的, 把美丽动人的玉照都顶下去了. 大家知道wordpress黑名单的评论会被自动标记为垃圾评论, 这个做法我感觉挺浪费资源的, 毕竟垃圾评论还是写入了数据库, 如果集中时间段被大量spam攻击, 这种黑名单机制对保护数据库无济于事, 然

其实伦家不想这么快就发新文章的, 把美丽动人的玉照都顶下去了.
大家知道wordpress黑名单的评论会被自动标记为垃圾评论, 这个做法我感觉挺浪费资源的, 毕竟垃圾评论还是写入了数据库, 如果集中时间段被大量spam攻击, 这种黑名单机制对保护数据库无济于事,
然后我写了个函数, 效果是直接拒绝符合黑名单条件的评论发表, 评论不会写入数据库

方法A : 以下代码扔到functions.php

function xhd_fuckspam($comment) {
    if(  is_user_logged_in()){ return $comment;} //登录用户无压力...
    if( wp_blacklist_check($comment['comment_author'],$comment['comment_author_email'],$comment['comment_author_url'], $comment['comment_content'], $comment['comment_author_IP'], $comment['comment_agent'] )){
        header("Content-type: text/html; charset=utf-8");
        exit('草你麻痹垃圾评论滚粗');
    }  else  {
        return $comment; 
    }
} 
add_filter('preprocess_comment', 'xhd_fuckspam');
Copy after login

方法B : 以下代码扔到comments-ajax.php (随便找个地方,比如检查评论太快那段函数后面)

 if( wp_blacklist_check($comment_author,$comment_author_email,$comment_author_url, $comment_content )&&!is_user_logged_in()){//登录用户还是无压力...
        err(__('草你麻痹垃圾评论滚粗'));   
    }
Copy after login

两个方法效果和原理略有不同,

  • 方法A:click submit→POST出去→收到拒绝信息 exit→return→ 会清空评论框内容并重置提交时间间隔 == submit了一次
    优点: 杀毒彻底
  • 方法B:click submit→POST被拒绝→return→ 会保留评论框内容 == 没有submit过
    优点:
    1. 评论者省时省力, 我是用此法来拒绝纯英文评论的, 因为此类误判几率大, 省得好人们重新再评论一次了;
    2. 由于没有POST出去, 所以相对而言对数据库压力最小;

    缺点: 由于comments-ajax是从表单内直接提取$_POST的的字段,所以无法匹配IP和agent类黑名单

效果图:
fuck-spam

可以自行测试, 方法: 把你在这里评论时的昵称改成带bags或者louis的, (这些都已经in_array在小蝴蝶黑名单了) 然后提交评论, 剩下来的你们就自己感受吧
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!