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 PHP Tutorials. Follow any responses to this post through RSS 2.0. You can skip to the end and leave a response. Pinging is currently not allowed. |
