Block access to site from specific IPs?

1.3k views Asked by At

I'm looking to try and block access from two IP addresses in PHP, preferably notifying them of their blocking.

Is there anyway I can do this or is it just not possible using PHP?

I've seen a JavaScript version of doing it, but if the browser has JavaScript turned off, then surely they'd be able to get around it.

Thanks for any advice/help in advance.

4

There are 4 answers

2
chaos On BEST ANSWER

in index.php:

if($_SERVER['REMOTE_ADDR'] == '10.0.0.1' || $_SERVER['REMOTE_ADDR'] == '10.0.0.2')
    die('Go away, banned person');

Or this version, which becomes more manageable when you start having more than one or two bans:

$bans = array(
    '10.0.0.1'  => true,
    '10.0.0.2'  => true,
);
if(!empty($bans[$_SERVER['REMOTE_ADDR']]))
    die('Go away, banned person');
0
krdluzni On

$_SERVER['REMOTE_ADDR'] should contain the IP.

1
Nick Gotch On

One place to block is on the web server. Then only allowed IPs would even make it to your site and you wouldn'ty have to introduce any special handling code.

See http://www.iphowto.com/Block_IP_How_To.aspx

0
Wade On

Take a look at this page. It contains a function for getting a client's IP address. You can call this on any page and if the IP address matches then display an error message.

However an easier solution is to use .htaccess

Add the following lines to .htaccess

order allow,deny
deny from xx.xx.xx.xxx
deny from xxx.xxx.xxx
allow from all

Where xxx are the IP number you wish to block.