Posted by: thebeanproject on: June 19, 2009
There are several ways of blocking specific IP Addresses. In this article I will talk about blocking IP Address using PHP, ASP, ASP.NET, and JavaScript.
PHP Code:
$blockedIP = array();
$blockedIP[] = “x.x.x.x”;
$blockedIP[] = “x.x.x.x”;
$blockedIP[] = “x.x.x.x”;
$blockedIP[] = “x.x.x.x”;$incomingIP = $_SERVER["REMOTE_ADDR"];
for($i=0; $i<count($blockedIP); $i++){
if($blockedIP[$i] == $incomingIP){
echo “<script>alert(‘you are not allowed to visit this site anymore’);</script>”;
header(“location: http://www.google.com”);
break;}
}
ASP Code:
Dim blockedIP(2) ‘fixed size array
blockedIP(0) = “x.x.x.x”
blockedIP(1) = “x.x.x.x”
blockedIP(2) = “x.x.x.x”Dim incomingIP
incomingIP = Request.ServerVariables(“REMOTE_ADDR”)For i=0 to UBound(blockedIP)
if(blockedIP(i) = incomingIP) then
response.write(“<script>alert(‘you are not allowed to visit this site anymore’);</script>”)
response.redirect(“http://www.google.com”)
exit forend if
Next
ASP.NET C# Code:
ArrayList blockedIP = new ArrayList();
blockedIP.Add(“x.x.x.x”);
blockedIP.Add(“x.x.x.x”);
blockedIP.Add(“x.x.x.x”);string incomingIP = Request.UserHostAddress;
foreach (string x in blockedIP){
if (x == incomingIP){
response.write(“<script>alert(‘you are not allowed to visit this site anymore’);</script>”);
response.redirect(“http://www.google.com”);
break;}
}
JavaScript Code:
There is a MAJOR FLAW using JavaScript to blocked specific ip address. If the JavaScript is turned off this script will NOT WORK!!.
<script language=”javascript” type=”text/javascript”>
var blockedIP = new Array();
blockedIP[0] = “x.x.x.x”;
blockedIP[1] = “x.x.x.x”;
blockedIP[2] = “x.x.x.x”;//PHP –> <?php echo $_SERVER["REMOTE_ADDR"]; ?>
//ASP –> <%= Request.ServerVariables(“REMOTE_ADDR”) %>
//ASP.NET –> <%= Request.UserHostAddress %>
var ip = ‘<?php echo $_SERVER["REMOTE_ADDR"]; ?>‘;for(var i=0; i<blockedIP.length; i++){
if(blockedIP[i] == ip){
alert(“you are not allowed to visit this site anymore”);
window.location = “http://www.google.com”;
break;}
}
</script>