-1

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?

L.Lovato
  • 31
  • 3

2 Answers2

0

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.

Ali Sharifi Neyestani
  • 4,162
  • 1
  • 14
  • 22
0

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();
}
  • Great!!! Very good! But in fact I wanted to block by city, not by country. With this method it is possible? Thxxx – L.Lovato Nov 19 '18 at 21:52
  • @L.Lovato no city is not possible. For city one thing you can do is to read the coordinates of the user by using javascript code (you can get it with a simple googling ) and then pass this coordinates to the server and there using the google api identify the address and check if the blocked city is in the address. One issue is that the user need to allow the location sharing in the browser or else this method will fail. Since user's location falls under privat information so there is a great change that you cannot get access to this information easily. – Azharuddin Laskar Nov 19 '18 at 22:00
  • Ok, thanks! I’ll try!! – L.Lovato Nov 21 '18 at 18:11