Block access to site from specific IPs?

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?

Read More

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.

Related posts

Leave a Reply

4 comments

  1. 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');
    
  2. 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.