简体   繁体   中英

Block access to website from IP or region

I am trying to block the website to be viewed as not exist from certain region. Is this possible?

I know we can just put a die() and a blank page will be shown, but is there a way to make it seen like this domain does not exist?

You can use the geoip mod for apache ( http://www.maxmind.com/app/mod_geoip ). Mod_rewrite rules can then determine how to handle the page.

RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ http://www.america.com$1 [F]

The F forbids page access

No, unless you have the DNS server for that domain under your control. Which you usually don't for domains like domain.tld, you can only adjust the content based on the clients ip - for example you can just serve an empty page as you suggested. But it is still possible to query the domain with tools like nslookup.

If you don't have access to the DNS server, you can add .htaccess rule to ban people from specific ipaddresses. Following are some sample .htaccess rules

order allow,deny
deny from 123.456.789.012     #block the visitors from the specific ipaddress 123.456.789.012
deny from 123.456.789.        #blocks the visitors from all ip within the range 123.456.789.xxx (i.e. 123.456.789.000 – 123.456.789.255)
deny from 123.456.            #blocks the visitors from all ip within the range 123.456.xxx.xxx
deny from 123.                #blocks the visitors from all ip within the range 123.xxx.xxx.xxx
allow from all                #allow from all other.
<?php

if (getenv(HTTP_X_FORWARDED_FOR)) {
    $pipaddress = getenv(HTTP_X_FORWARDED_FOR);
    $ipaddress = getenv(REMOTE_ADDR);
echo "Your Proxy IPaddress is : ".$pipaddress. "(via $ipaddress)" ;
} else {
    $ipaddress = getenv(REMOTE_ADDR);
    echo "Your IP address is : $ipaddress";
}
?>

This code u can use to get ip address of visitor...

To detect region you'll need some free api.. Try this..

This Api can also be used..

The second one is really easy to use..

<A HREF="http://www.hostip.info">
 <IMG SRC="http://api.hostip.info/flag.php?ip=12.215.42.19" ALT="IP Address Lookup">
</A>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM