不用插件屏蔽 WordPress 垃圾评论(二)

前天晚间博客遭受了一波垃圾评论攻击,攻击者大概是看了我之前的一篇介绍在用的防垃圾评论的日志,知道我这里评论文本框的名字叫 little_star,因此哥们一夜一间就制造了几百条垃圾评论。真是出乎意料,我一直觉得没人会对我的无名小站感兴趣,没想到这一天还是来了。

因此,这两天对在用的防垃圾评论机制进行了一些改进,总体思路是生成动态的评论文本框名称。方法大概入如下:
在主题 function.php 文件中加入以下几个函数

// 生成随机字符串
function get_brave_hash($hash_length = NULL) {
	$hash_length = !is_int($hash_length) ? rand(8,14) : abs($hash_length);
	return substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"), -$hash_length);
}

session_start(); // 开启 session

// 生成评论框名称
function get_comment_text_name() {
	$comment_text_name = 'real_comment';
	$comment_text_name_hash = get_array_key('_SESSION', $comment_text_name);
	if ( isset($comment_text_name_hash) ) {
		// session 里存在时直接返回 $_SESSION 中存储的值
		return $comment_text_name_hash;
	}
	$sep = '_';
	$hash_length = 8; // 8 位长度
	$comment_text_name_hash = $comment_text_name . $sep . get_brave_hash($hash_length);
	// 加入到 session
	$_SESSION[$comment_text_name] = $comment_text_name_hash;
	return $comment_text_name_hash;
}

// 获取数组键值
function get_array_key($array, $key) {
	if (is_string($array)) {
		global $$array;
		return array_key_exists($key, $$array) ? $$array[$key] : null;
	}
	return array_key_exists($key, $array) ? $array[$key] : null;
}

在主题评论文件中(我这里是 comments.php)调用 get_comment_text_name() 函数,生成动态的名称,用作评论文本框名称:

<?php
$comment_text_name = get_comment_text_name();
comment_form( array(
'fields' => array(
'author' => '<p class="commentform"><input type="text" name="author" id="author" class="inp cmt-input" placeholder="昵称 [必填]" aria-required="true" size="30" value="' . esc_attr( $commenter['comment_author'] ) . '"></p>',
'email' => '<p class="commentform"><input type="text" name="email" id="email" class="inp cmt-input" placeholder="邮箱 [必填·保密]" aria-required="true" size="30" value="' . esc_attr( $commenter['comment_author_email'] ) . '"></p>',
'url' => '<p class="commentform"><input type="text" name="url" id="url" class="inp cmt-input" placeholder="网址 [选填]" size="30" value="'.$comment_author_url.'"></p>',
'cookies' => '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' />' .
'<label for="wp-comment-cookies-consent" class="c4"> 记住我的个人信息</label></p>'
),
'comment_field' => '<textarea id="comment" name="comment"></textarea>
<textarea name= ' . '"' .$comment_text_name . '"' . ' class="inp text-bg comment-textarea" placeholder="添加评论..." aria-required="true" cols="45" rows="8"></textarea>',
'title_reply' => '发表评论',
'cancel_reply_link' => '取消回复',
'label_submit' => '发表评论',
'comment_notes_before' => '',
'comment_notes_after' => '',
'class_submit' => 'btn submit',
'action' => '/wp-stop-spam.php'
));
?>

在 wp-stop-spam.php 文件中通过 session 获取评论框名称,并进行置换

<?php
/** Sets up the WordPress Environment. */
require_once __DIR__ . '/wp-load.php';
nocache_headers();

$comment = trim($_POST['comment']);
if ( empty( $comment ) ) {
// 置换评论内容
$comment_text_name = get_comment_text_name();
$_POST['comment'] = trim($_POST[$comment_text_name]);
require_once( __DIR__ . '/wp-comments-post.php' );
}
if ( ! empty( $comment ) ) {
	// 返回错误
	wp_die('放过我吧!');
}

发表评论