Reader
  • Home
  • About
  • Deals
  • Services
  • Contact

Advanced IP Ban PHP Script

Last updated on July 9th, 2009 by Gabriel Livan

Hi,

This script is useful if you want to restrict the access to some people on your site, based on their IPs. There are 2 lists which can be used: one which has a list of the IPs you want to ban and one which you can use to ban a range of IPs. The script checks both lists and if the Visitor’s IP is in the list then the script will show a message to the user and exit.

<?php
/* List with IPs */
$ban_ip_list = array('68.180.206.184', '64.233.167.99', '207.46.232.182');

/* List with IP ranges. Use the '*' as the range selector */
$ban_ip_range = array('69.*.83.197');

/* Visitor's IP Address */
$user_ip = $_SERVER['REMOTE_ADDR'];

/* Message to output if the IP is in the ban list */
$msg = 'You do not have permission to access this page.';

/* Message to output if the IP is in the ban list */

    if(in_array($user_ip, $ban_ip_list))
	{
	  exit($msg);
	}

/* Check if the Visitor's IP is in our range's list */

if(!empty($ban_ip_range))
{
foreach($ban_ip_range as $range)
{
	$range = str_replace('*','(.*)', $range);

    if(preg_match('/'.$range.'/', $user_ip))
	{
	  exit($msg);
	}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>Welcome to My site</TITLE>
 </HEAD>
 <BODY>

  Here is my site content.
 </BODY>
</HTML>

Filed Under: PHP

About Me

Gabriel LivanHey, there! I'm Gabriel Livan, Web Developer, Blogger and Internet Marketer. I love making fast, secure and scalable web applications, running and travelling around the world. I blog about WordPress, site speed & security and quality tools for web professionals. Read more

What are you looking for?

Categories

  • AJAX (22)
  • Deals (10)
  • Design (30)
  • eBooks (2)
  • Flash (19)
  • Freebies (11)
  • General (62)
  • Giveaways (1)
  • Graphics (27)
  • HTML & CSS (35)
    • Frameworks (9)
  • JavaScript (160)
    • Frameworks (8)
    • jQuery (89)
    • MooTools (14)
  • Page Loading Speed (2)
  • Photoshop (3)
  • PHP (113)
  • Reviews (8)
  • SEO (6)
  • Statistics (2)
  • Web Tools (13)
  • WordPress (27)
    • Plugins (15)
    • Snippets (4)
    • Tools (2)
    • Tutorials (6)

Copyright © 2026 · BitRepository.com - All Rights Reserved

  • Home
  • About
  • Contact