I want to block certain IP ranges / Regions / Country from accessing some of my pages

548 Views Asked by At

I do not want my complete site to be blocked in a country, instead I want to block some countries or IPs from accessing some pages on site.

If someone can just give me a push in the right direction that would be great.

1

There are 1 best solutions below

1
On

You did not specify the language you are using, but you can either use a local IP to country database or query an API and reject the visitor if its country code matches your block list :

$country = file_get_contents("http://api.db-ip.com/v2/free/{$_SERVER['REMOTE_ADDR']}/countryCode");
if (in_array($country, [ "US", "CA" ])) {
    header("Status: 403 Forbidden");
    die();
}