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,796
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,796
Reactions
406
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom