自动拦截垃圾评论,加强WordPress博客安全

大家都知道,我们做博客的都很讨厌垃圾评论,有时泛滥起来真的让人防不胜防,然而,可喜的是,Wordpress有一款相当不错的防垃圾评论插件Akismet,对抗垃圾评论很有一手,但是,无论多强大的插件都会存在或多或少的缺点,Akismet当然也不例外,所以这一篇文章就介绍一种方法,在防垃圾评论方面可以弥补使用插件的不足,让我们的Wordpress博客更安全。

将以下的代码复制并粘贴进主题的functions.php中:

function in_comment_post_like($string, $array) {
foreach($array as $ref) { if(strstr($string, $ref)) { return true; } }
return false;
}
function drop_bad_comments() {
if (!empty($_POST['comment'])) {
$post_comment_content = $_POST['comment'];
$lower_case_comment = strtolower($_POST['comment']);
$bad_comment_content = array(
'viagra',
'hydrocodone',
'hair loss',
'[url=http',
'[link=http',
'xanax',
'tramadol',
'russian girls',
'russian brides',
'lorazepam',
'adderall',
'dexadrine',
'no prescription',
'oxycontin',
'without a prescription',
'sex pics',
'family incest',
'online casinos',
'online dating',
'cialis',
'best forex',
'amoxicillin'
);
if (in_comment_post_like($lower_case_comment, $bad_comment_content)) {
$comment_box_text = wordwrap(trim($post_comment_content), 80, "\n ", true);
$txtdrop = fopen('/var/log/httpd/wp_post_logger/nullamatix.com_text_area_dropped.txt', 'a');
fwrite($txtdrop, " --------------\n [COMMENT] = " . $post_comment_content . "\n --------------\n");
fwrite($txtdrop, " [SOURCE_IP] = " . $_SERVER['REMOTE_ADDR'] . " @ " . date("F j, Y, g:i a") . "\n");
fwrite($txtdrop, " [USERAGENT] = " . $_SERVER['HTTP_USER_AGENT'] . "\n");
fwrite($txtdrop, " [REFERER ] = " . $_SERVER['HTTP_REFERER'] . "\n");
fwrite($txtdrop, " [FILE_NAME] = " . $_SERVER['SCRIPT_NAME'] . " - [REQ_URI] = " . $_SERVER['REQUEST_URI'] . "\n");
fwrite($txtdrop, '--------------**********------------------'."\n");
header("HTTP/1.1 406 Not Acceptable");
header("Status: 406 Not Acceptable");
header("Connection: Close");
wp_die( __('bang bang.') );
}
}
}
add_action('init', 'drop_bad_comments');

凡里面包含有$bad_comment_content数组中所列出词语的评论都会被系统自动拒之门外,将这个功能整合到你的Wordpress博客中,即使不用插件,你的博客也会减少很多垃圾评论的。

如无特别说明,本站文章皆为原创,若要转载,必须注明以下原文信息:
日志标题: 《自动拦截垃圾评论,加强WordPress博客安全》
日志链接: http://www.chenchunlin.info/automatically-refuse-spam-comments-on-your-wordpress-blog.html
博客名称: Chenchunlinblog

This entry was posted in WordPress and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

欢迎发表评论

我们不会公开您的邮箱地址, 标有*号的是必填项哦!

*
*

您可以使用这些HTML标签及其属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>