views:

121

answers:

4

I'm looking to try and block access from 2 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 Javacript turned off, then surely they'd be able to get around it.

thanks for any advice/help in advance.

+2  A: 

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');
chaos
VoteyDisciple
Yeah. Sometimes I still think in Perl. Edited to go one better. :)
chaos
+1  A: 

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

krdluzni
A: 

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

Nick Gotch
He wanted to notify the user of the ban. Can this be done from the web server?
krdluzni
+2  A: 

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.

Wade