Good afternoon! I would like to know how I can block my site in some regions by PHP. I do not want my site to run in all regions Can someone help me?
How block site by region
831 views Asked by L.Lovato At
2
There are 2 answers
3
On
Try to get the ip of the connection party using $_SERVER['REMOTE_ADDR'] and check if the country of the IP is in your block list. Here is a code snippet that can help you
$cip = $_SERVER['REMOTE_ADDR'];
$iptolocation = 'http://api.hostip.info/country.php?ip=' . $cip;
$creatorcountry = file_get_contents($iptolocation);
$creatorcountry will contain the country of the connection party which you can look against a list of blocked country and then return false as response
if (in_array(strtolower($creatorcountry), $blockedCountryList)) {
echo "Your country is blocked."
exit();
}
The best way to do this is to use a firewall. Another way is to use Apache. An easy way to do this is to use this link.
With this tool you can select the countries you do not intend to serve, which gives you the default settings.
Of course, I hope you do not intend to block your services to Iranians :)
If you want to try blocking the city nonetheless, take a look at mod_security +GeoIP (assuming you're using Apache).
Updated:
you can to use IptoLocation Script(http://www.ip2location.com/) and then you can check the city or region after that Blocking a City or Region from website. You can query block information at http://location2ipaddress.com/ for example. If you want to use the .htaccess file to do this, then all you need is the ip range data - put this into the deny list and you're done.
Furthermore you can use any geoIP API to query the location of the visitor. Just google geoIP API to see what's available. There are online solutions and downloadable databases as well.