IP Banning

Warning: This article/tutorial is more the 45 days old. As such the information contained within could be, by now, out of date. Please read all information to make sure that this article/tutorial will work with your current version of the Operating System

Blocking certain IP addresses from your site is actually much easier than you might think.

In fact, it is only a seven-line code inserted at the top of your page!

The code:

1 :

Please note that the used IP addresses are randomly selected numbers.

Now, for the explanation of the code.

Line 1 simply opens the PHP code.

Line 2 sets the variable $banned to an array containing all the IP addresses you want to be blocked from your site.

Line 3 opens a conditional. The conditional uses the in_array() function. This function checks if the first argument is in the second argument, which is an array. Our first argument, $_SERVER[‘REMOTE_ADDR’], is automatically set to the viewer’s IP address.

Line 4 echoes the message “You have been banned” if the user’s IP address is in the array $banned.

Line 5 exits the page, not executing any code below line 5 if the above conditions are met.

Line 6 ends the conditional.

Line 7 ends the PHP code.

So, if you are implementing this code into your page, just put it above the HTML tag like below:

My Page

Main content

And that is the simple code that blocks chosen IP addresses from your site.

Leave a comment

Your email address will not be published. Required fields are marked *