wordpress广告垃圾评论过滤(验证码、关键词)

作者:袖梨 2022-06-25

方法一,给评论增加算术验证码

在主题目录的functions.php添加如下代码:

代码如下 复制代码

//算术验证码by vfhky
function spam_provent_math(){
$a=rand(5,15);
$b=rand(5,15);
echo " = $a + $b (防止机器人评论)" ."" ."";
}
function spam_provent_pre($spam_result){
$sum=$_POST['sum'];
switch($sum){
case $_POST['a']+$_POST['b']:break;
case null:wp_die('亲,算个结果撒');break;
default:wp_die('算错啦⊙?⊙b汗');
}
return $spam_result;
}
//注册用户or管理员则不需要验证
if(!is_user_logged_in() && $comment_data['comment_type']==''){
add_filter('preprocess_comment','spam_provent_pre');
}

(二)在主题目录下的comments.php(不同的主题可能评论框的位置不同,有的主题可能在functions.php里面)中调用上述代码:

代码如下 复制代码

//根据是否是管理员来决定是否显示验证码
<?php if(!isset($_COOKIE['comment_author_email_'.COOKIEHASH]))spam_provent_math();?>


方法二,利用程序进行关键过滤

在你的主题下面的functions.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);
}
}
// ??谖?br /> 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>",$input);') );
}
}
}
// ?z查
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(); 前面?尚本??h除即可.
//die();
// 方法二: ?擞?? spam, 留在?料??z查是否?判.
add_filter('pre_comment_approved', create_function('', 'return "spam";'));
$comment['comment_content'] = "[ 小??判?噙@是Spam! ]n". $_POST['spam_confirmed'];
$this->add_black( $comment );
} else {
// ?z查?像
$f = md5( strtolower($comment['comment_author_email']) );
$g = sprintf( "http://%d.grav*a*t*ar.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;
}
// 列入黑名??br /> 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();
// -- END ----------------------------------------

最后记得把代码$2w$3$4中间的w和$w = 'w'的w改成其它英文字母(但是二者要一致),比如$2wc$3和$w = 'wc'等等,千万不要让spam发现额^^

相关文章

精彩推荐