Blacklist IPs from viewing your webpage

This is a simple small piece of code that allows you to ban visitors fro viewing your webpage, if you know their ip adresses…

This can be made much more advanced.

With the use of a database such as Mysq (or in some cases just a textfile),  you can create a management system for adding, editing and deleting blacklisted ips.

1
2
3
4
5
6
7
8
9
10
11
<?PHP
$ip = $_SERVER['REMOTE_ADDR'];
$ban = array(
"74.89.41.93",
"123.40.1.63",
);
 
if(in_array($ip, $ban)){
die("Sorry, you are banned!");
}
?>

Explained:

The first line, gets the visitors ip, the second line creates an
array of the blacklisted ip adresses.

The IF statement, checks if the visitors ip is blacklisted, if true, it stops loading
of the webpage and shows the error message.

Usage:

Put the code at the very top of your .php document.

If anyone want a more advanced method to do this (with all the editing and stuff), drop me a comment below :)

2 Responses to “Blacklist IPs from viewing your webpage”

  1. Ausome1 Says:

    You should also post your code on 2020code.com to promote your site and your code.

  2. Ratman87 Says:

    Hi! I will take a look at it :) the site is in the srart phase right now, but Its going well :) thank you for the tip!

Leave a Reply


To prove that you're not a bot, enter this code
Anti-Spam Image

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>