IP Block

By Kerrie on Aug 23, 2009

Use this script to block certain IPs from your webpage, by redirecting them to another site. Note that your web server must have SSI enabled (most Linux servers do), with the page using the correct extension (ie: .shtml) in order for this script to work. Furthermore, since this script relies on JavaScript to block the user (by redirecting the person to another site), if he/she disables JavaScript in the browser, the script will become futile.

<script type="text/javascript"

//Enter list of banned ips, each separated with a comma:
var bannedips=["23.23.23.23", "11.11.11.11"]

var ip = '<!--#echo var="REMOTE_ADDR"-->'

var handleips=bannedips.join("|")
handleips=new RegExp(handleips, "i")

if (ip.search(handleips)!=-1){ 
alert("Your IP has been banned from this site. Redirecting...")
window.location.replace("http://www.google.com")
}

</script>

Comments

Sign in to comment.
Korvin   -  Mar 02, 2010
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$bips = array('192.168.1.1','10.0.0.1');
if (in_array($ip, $bips)) {
    header('Location: http://google.com');
}
?>
aboutscript  -  Jan 05, 2013

Might want to put die(); after the header call. Otherwise the rest of the page can still render and the user receives the content.

Sign in to comment

Jonesy44   -  Aug 23, 2009

you should re-write this in PHP, that way the server handles the code execution, there's nothing the client can do to avoid the "block". This is not secure.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.