Nov172013
利用代码使WordPress博客自动禁止包含特定关键词的垃圾评论
针对目前WordPress博客的垃圾评论依旧来势汹涌,因此垃圾站博客今天的主要话题依旧是如何防范垃圾评论!
方法简介:将以下代码添加到WordPress博客主题的functions.php文件中,根据自己的需要修改 $ bad_comment_content 数组的内容,任何包含在$ bad_comment_content 数组内的字符都将会被自动拒绝留言!
代码如下:
//拒绝包含特定关键词的垃圾评论 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( '338c.com', 'baige88.com', '1e68.com', 'www.dwz.cn', 'is' ); 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');
PS:魔高一尺道高一丈,针对垃圾评论必须来狠的,多管齐下才是好办法!具体方法请在垃圾站博客搜索“评论”查询,注意修改代码前记得备份!
本文由垃圾站编辑整理,转载请注明原文地址:http://www.lajiz.cn/2131.html
利用代码使WordPress博客自动禁止包含特定关键词的垃圾评论:等您坐沙发呢!
发表评论
