IpForensics
HomeGoto Dashboard
  • 📖Introduction
  • Protocols
    • HTTP Json Protocol
    • Response headers
    • Error Codes
  • Features
    • Pay-As-You-Go
    • API Security
    • HTTPS
  • Api Response Data
    • 📶Mobile Carrier
    • 🏥ASN
    • 📊General
    • ⏲️Timezone
    • 💵Currency
    • 👨‍💻Threat Detector
    • 🌐Company
    • 🌏Geolocation
    • 🌍Advance Geolocation
    • 💻Device Information
    • ✈️IATA/ICAO
  • API
    • 🚀Intro
    • 📊IP & Threat Intelligence API
      • Single Lookup
      • Origin Lookup
    • 💹Exchange Rate API
      • Currency Endpoint
      • Live Rate Endpoint
      • Historical Rate Endpoint
      • Currency Conversion Endpoint
      • Currency Swap Endpoint
  • Code Implantation
    • Block Automated Visits From Hosting Providers
    • Block VPN/Proxy and Tor users
    • Redirect by Country and Location Offers
    • Content Personalisation
    • Get the location from an IP Address in Javascript
    • How to get a client's IP address using JavaScript
    • HTML5 Geolocation with IpForensics fallback
    • Blocking Users by Country
    • Detecting Users Currency or Currency Personalisation
    • Preventing Free Trial Abuse
    • Device-Based Ads and App Binary
    • Detect Users Device/Computer and Operating System
    • Detect visitors from EU countries
    • How to detect and prevent credit card fraud
    • How to convert amounts to website visitor currency
  • MISC
    • Changelog
    • FAQs
    • Troubleshooting
    • API Status
    • Rate Limits
Powered by GitBook
On this page

Was this helpful?

  1. Code Implantation

Blocking Users by Country

PreviousHTML5 Geolocation with IpForensics fallbackNextDetecting Users Currency or Currency Personalisation

Last updated 2 years ago

Was this helpful?

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 .

// List of countries we want to block
// To see this in action add your country code to the array
var blacklist = ['ZM', 'CA', 'CH', 'IN']

// Getting the country code from the user's IP
$.get("https://ipforensics.net/api/v1/origin?apikey=APIKEY", function (response) {

  // Checking if the user's country code is in the blacklist
  // You could inverse the logic here to use a whitelist instead
  if (blacklist.includes(response.meta_data.geolocation.code)) {
    alert('This content is not available at your location.');
  } else {
    alert("You're allowed to see this!")
  }
}, "jsonp");

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.

Always reference the for complete response result

ISO 3166 Alpha-2
API endpoint