Automatic IP Blacklist

Post Reply
User avatar

Topic Author
Kygo <>
Founder
Founder
Posts: 140
Joined: 26 Sep 2024, 21:31
Location: Romania
Has thanked: 1 time
Been thanked: 15 times
Contact:
    Windows 10 Edge
Dec 2024 29 13:06

Automatic IP Blacklist

Post by Kygo <>

Step 1: Add PHP Script

To implement the automatic IP blacklist, first upload the script to your server. You can copy paste from the following code.
So without further ado, here is the Automatic IP Blacklist:
Please Login or Register to see this code No changes need to made, simply upload and then proceed to step 2 for the cron job. By itself this script doesn’t do anything. It needs to be called in order to lookup and grab the latest blocked IP addresses and update your site’s root file. So technically you could just manually call the script via your browser every day, but that would be stupid when we can automate the process via simple cron command..htaccess

Step 2: Cron Job

After uploading the automatic IP blacklist to your server, you need to set up a cron job to run the script every day or week or whatever you want. There are at least two ways to set up a cron job: directly via your server (or server control panel) OR using WordPress Cron API. Let’s look at an example of each.

Linux/Unix Cron

Cron is a chronological scheduling service in Linux/Unix operating systems. Cron jobs (crontab commands) are used to execute commands or scripts periodically, at specified time intervals. In Linux/Unix, the cron service (daemon) runs in the background and constantly polls the file, , and directories. /etc/crontab/var/spool/cron//etc/cron./*

A typical cron job that runs every 24 hours looks like this:
Please Login or Register to see this code Without derailing into an in-depth tutorial on setting up cron jobs, suffice it to say that there are three thing you need to get right in order for this to work:

The time interval
The path to the resource (in this case, PHP)
The path the script (in this case, auto IP blacklist)
I can tell you from hard-earned experience that the tricky part to setting up cron jobs is getting the file paths correct. Once everything is setup, however, cron will not fail you. Check out this beginner’s guide for more information about setting up and configuring cron jobs. (Lots more great tutorials out there too).

WordPress Cron

Like Linux/Unix Cron, WP Cron enables us to perform time-based tasks at recurring intervals. Unlike Linux/Unix Cron, WP Cron is a pseudo cron service. Instead of running constantly, WP Cron runs only on page load. So when a page is loaded, WP Cron checks the queue of scheduled tasks and runs anything that is past the scheduled time. In most cases this pseudo approach works well; but for sites with little activity, the queue of scheduled events can build up.

Here is a super basic script for setting up a new WP Cron event:
Please Login or Register to see this code The only change that needs to be made: change the to match the actual path to the . To add these two functions to WordPress, you can create a simple plugin (recommended) or add the code to your active theme’s ./path/to/automatic-ip-blacklist.phpfunctions.php

Testing
After implementing the automatic IP blacklist, you can verify that everything is working by simply keeping an eye on your site’s root file. You should notice that the IP-blocking directives change every day. .htaccess

So to test functionality while getting set up, you can decrease the time interval in your cron job to something like 5 minutes or whatever short duration. Then you can add a comment or empty line to the block-IP rules in your .htaccess file. Wait five or whatever minutes and then check to see if the comment or empty line is removed. If so, that means the blocked IP rules were successfully updated (and in the process your comment or blank line was overwritten/removed).

Download
Grab a copy of the automatic IP blacklist.
automatic-ip-blacklist.zip

You do not have the required permissions to view the files attached to this post! Maybe your post count is too low.
Image
Post Reply