WordPress自动封垃圾邮件IP的思路和实现方法

2016年08月21日

原创内容,转载请注明出处: https://www.myzhenai.com.cn/post/2287.html https://www.myzhenai.com/thread-17912-1-1.html
关键词: wordpress垃圾回复 wordpress封ip iptables封IP wordpress自动封IP wordpress自动封发垃圾邮件的IP
众所周知,WordPress有一个自动拦截垃圾回复的插件Akismet 但是这个插件有一个不方便的地方就是它只是在垃圾回复发布了才进行拦截, 但是这个难免也会涉及到数据库写入和删除,这无疑会增加服务器的负担, 所以我的思路是调用Akismet拦截垃圾回复的IP地址,并且用Shell脚本把这些IP写入到iptables防火墙过滤名单里. 当再收到这些IP发来的请求时,系统会自动将它们丢弃,不会进入系统操作了. 实现起来很简单, 现在我们来说一说实现的步骤.
一: 将以下代码添加到 /wp-admin/includes/class-wp-comments-list-table.php 的指定位置里, 有两个地方,一个是自定义函数,一个是添加自定义函数.
参考: WordPress获取垃圾回复IP地址的方法 https://www.myzhenai.com.cn/post/1875.html https://www.myzhenai.com/thread-16324-1-1.html

/** chong zhe li kai chi */
function my_spam_ip(){
    $aip = get_comment_author_IP(); /** 得到ip地址 */
    $txt = file_get_contents("/var/wwwroot/spam.log"); /** 对比ip地址是否存在文件内,防止重复输入 */
    $txt = preg_replace("/(\r\n|\n|\r|\t)/i", '', $txt);
    if (strpos($txt,$aip) === false) /** 如果文件内找不到这个ip地址 */
    {
        $handle = fopen("/var/wwwroot/spam.log","a+"); /** 打开文件并得到一个句柄,据说a+是写入? */
        fwrite($handle,$aip."\n"); /** 将获得的ip地址添加到文件中 */
    }
    echo "Spam IP: ".$aip."\n";
    fclose($handle); /** 关闭句柄 */
    }
/** dao zhe li jie shu  */

 
将这段代码添加在 /wp-admin/includes/class-wp-comments-list-table.php 文件里的 < ?php ?>符号内, 然后再将 my_spam_ip(); 这句添加到下行的下方.

$author_ip_url = add_query_arg( 'comment_status', 'spam', $author_ip_url );

 

class-wp-comments-list-table.php

class-wp-comments-list-table.php


class-wp-comments-list-table.php

class-wp-comments-list-table.php


二: 将以下Shell脚本代码保存为.sh后缀的脚本文件,上传到服务器指定目录, 然后在cron里添加一条定时任务.
参考: Linux下使用crontab添加定时任务 https://www.myzhenai.com.cn/post/1628.html

# !/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
path="/var/wwwroot/spam.log"
size=`ls -l $path|awk '{print $5}'`
if [ $size -gt 0 ];then
for i in $(< $path)
do
if [ $i != "" ];then
h=`grep -c $i /etc/sysconfig/iptables`
if [ $h -lt 1 ];then
iptables -I INPUT -s $i -j DROP
fi
fi
done
service iptables save
service iptables restart
IFS=$SAVEIFS
echo -n "" > $path
chown -R apache:apache $path
chmod 777 $path
fi

 

Shell脚本

Shell脚本


至此,设置完毕,然后你就会发现WordPress后台接收到垃圾回复就会越来越少了,因为只要你删除了的垃圾回复系统就会自动将它的IP地址进行过滤了.


sicnature ---------------------------------------------------------------------
Your current IP address is: 3.215.183.194
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://www.myzhenai.com/post/2287.html

2条评论

  • fancy 2016年09月1日在5:03 下午

    服务器环境是lnmpa,搭建了WordPress,nginx也正确配置了ssl,然后我到后台设置,常规,把WordPress地址和站点地址从http改成了https。那么问题就来了,我这样设置后,后台就打不开了,就连登陆页面也打不开了。提示网页存在重复跳转。。。。
    如果用lnmp,完全按照以上的步骤就能够正常配置。。。
    求大佬指导下,lnmpa的要怎么设置

    • 海南胡说 2016年09月3日在1:46 上午

      在.htaccess 里再添加一个强制http跳转到https看看
      代码如下
      RewriteEngine On
      RewriteCond %{SERVER_PORT} 80
      RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
      如果是在子目录,可以用
      RewriteEngine On
      RewriteCond %{SERVER_PORT} 80
      RewriteCond %{REQUEST_URI} subfolder
      RewriteRule ^(.*)$ https://www.domain.com/subfolder [R,L]
      将 http 访问强制重定向至 https,代码如下:
      RewriteEngine on
      RewriteBase /
      RewriteCond %{SERVER_PORT} !^443$
      RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
      将以上复制到.htaccess中即可。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注