Blocking Users by Country
This example shows you how to restrict your content from a list of countries.
We use the ISO 3166 ALPHA-2 Country Codes. You can curate a blacklist or whitelist of your own by looking up the countries you'd like to include at ISO 3166 Alpha-2.
1
// List of countries we want to block
2
// To see this in action add your country code to the array
3
var blacklist = ['US', 'CA', 'UK', 'IN']
4
5
// Getting the country code from the user's IP
6
$.get("https://api.ipdata.co?api-key=test", function (response) {
7
8
// Checking if the user's country code is in the blacklist
9
// You could inverse the logic here to use a whitelist instead
10
if (blacklist.includes(response.country_code)) {
11
alert('This content is not available at your location.');
12
} else {
13
alert("You're allowed to see this!")
14
}
15
}, "jsonp");
Copied!
This simple example shows an alert with This content is not available at your location if the user is from a country in the blacklist.
Alternatively it shows an alert with You're allowed to see this! for users that are not from the countries in the blacklist.
Last modified 2yr ago
Copy link