Simple PHP Ban IP Address Script
This script is a simple way to ban one IP address or a range of IP addresses from your website. The code uses comments to include detailed information on how the script works to make it easier for you to edit it as you need it.
Here is the code:
<?php
// IP to ban
$banned_ip = "1.2.3.4";
// user's IP is held in the $_SERVER variable
$user_ip = $_SERVER['REMOTE_ADDR'];
// check if user's IP matches banned IP
if($user_ip == $banned_ip) {
echo "Access denied";
}
// multiple IP addresses?
$banned_ips = array("1.2.3.4", "4.3.2.1");
// check for match
foreach($banned_ips as $ip_ban) {
if($user_ip == $ip_ban) {
echo "Access denied";
}
}
?>
| Print article | This entry was posted by kpac on July 18, 2010 at 3:17 pm, and is filed under .htaccess Tutorials, Apache Tutorials, CSS Tutorials, Design Software Tutorials, FTP Clients, Google Tutorials, HTML Tutorials, JavaScript Tutorials, PHP Tutorials, SEO Tutorials, Web Hosting Tutorials, Website Administration Tutorials. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |
Comments are closed.

about 2 years ago
It’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.