Sunday, May 23, 2010

How do I block IP address ranges from viewing my personal website?

I%26#039;ve been using the JavaScript found here: http://www.javascriptkit.com/script/scri... and it works for individual IP addresses, but have not figured out how to write the formula for a range of IP numbers. I would like to block the range 166.128.0.0 - 166.255.255.255.



Note: My web host is Yahoo! and they do not give their users access to the .htaccess file, that%26#039;s why I%26#039;m using JavaScript code on the page instead.



How do I block IP address ranges from viewing my personal website?





%26lt;html%26gt;



%26lt;head%26gt;



%26lt;script%26gt;



// block a range or ranges of ip addresses



// use SSI to parse ip address into page, assigning



// resulting value to global JavaScript variable



var ipAddress = %26#039;%26lt;!--#echo var=%26quot;REMOTE_ADDR%26quot;--%26gt;%26#039;;



ipAddress = formatQuadTripleIp(ipAddress);



ipAddress = parseFloat(ipAddress);



// give a list of range bounds for ip addresses:



// each item is a pair of ip addresses as



// [lower bound, upper bound]



// each bound pair should be a comma-separated



// pair of ip addresses enclosed in



// square brackets and separated from



// the following pair by a comma



// each ip bound value should be a complete



// quad-triplet as %26quot;###nnn###nnn%26quot;.



var ipBoundPairs = [



[%26#039;001002003004%26#039;, %26#039;001002003999%26#039;],



[%26#039;011002003004%26#039;, %26#039;011002003999%26#039;],



[%26#039;101002003004%26#039;, %26#039;101002003999%26#039;]



]



var last = ipBoundPairs.length;



var banned = false;



for (var i = 0; i %26lt; last; i++) {



var lower = ipBoundPairs[i][0];



lower = parseFloat(lower);



var upper = ipBoundPairs[i][1];



var upper = parseFloat(upper);



banned = banned ||



(ipAddress %26gt;= lower %26amp;%26amp;



ipAddress %26lt;= upper );



if (banned) break;



}



if (banned) {



alert(%26quot;You are not permitted to view this page!%26quot;);



window.location = http://www.fbi.gov%26quot;%26#039;;



}



else {



alert(%26#039;you are not banned%26#039;);



}



function formatQuadTripleIp(ip) {



ip = ip.split(%26#039;.%26#039;);



var fip = %26#039;%26#039;



for (var i = 0; i %26lt; 4; i++) {



fip += (%26#039;000%26#039; + ip[i]).slice(ip[i].length);



}



return fip;



}



%26lt;/script%26gt;



%26lt;/head%26gt;



%26lt;body%26gt;



%26lt;/body%26gt;



%26lt;/html%26gt;



How do I block IP address ranges from viewing my personal website?



You%26#039;ll need to fill an %26quot;banned%26quot; array with the range of IPs using a for statement. It%26#039;s going to be a large array. :P



Then using a DO statement you can check to see if their IP is in that list.



edit:



You know -- I haven%26#039;t programmed forever, otherwise I wouldn%26#039;t have provided a code example. But I was thinking, you can break the IP up into the first ocet. %26quot;166%26quot; and ban any or all IPs that start with 166.



Then again, Javascript banning isn%26#039;t fool proof by any means.



Does Yahoo support PHP or CGI Perl?



If so, you can create one single script that will check the IPs, You can call that script from every page on your site. And have a redirect page giving a %26quot;ban message%26quot;.



It wouldn%26#039;t require a whole lot of modification to your pages.



And the only way to bypass it would be to use a proxy or another connection



Other Replys:did not know no access to htaccess file on yahoo... that%26#039;s bad.



i have hosting and give client access to modify their htaccess. move your page to another company. u can find already for 1 usd a month. is 12 usd a year much? think no.

No comments:

Post a Comment