[PHP Snipet] Block Single IP or IP Range in PHP array

Tango

The President
3,622
969
113
2009
2,000
This is a simple bit of code you can paste into a PHP file to ban single Ips or Ranges by adding to the blocked_ips Array, it could be adapted for many uses.


PHP:
//// Add full IP or range as shown below. ////
$blocked_ips = array(
    "10.20.30",
    "11.12.13.14"
);

$sip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];

if (in_array(substr($sip, 0, strrpos($sip, '.')), $blocked_ips) || in_array($sip , $blocked_ips))
    {
    //// Add whatever code you want for banned IPs ////
    die("Your IP has been banned from this page");
    }
 
Top Bottom