Skip to content
WJunction - Webmaster Forum

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

Status
Not open for further replies.

Tango

Moderator
Staff member
4,148
2009
1,795
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");
    }
 
Status
Not open for further replies.

About the author

Tango
Moderator · Joined
4,148
Messages
1,795
Reactions
113
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom