Geo-Block the Visitors to Your Online Applications

Sheharyar Malik
By Sheharyar Malik Senior Software Engineer
Posted on February 22, 2024 | 3 min read
Geo-Block the Visitors to Your Online Applications
Back to All Posts

Online applications face many cyberattacks and exploits. Some online applications or some of their content are not available in some regions of the world. You can geo-restrict visitors from unavailable zones on your application using IP location services like the IPGeolocation.io API.

The steps to avoid unauthorized access in the restricted regions using IP location services would be:

  1. If a user visits a page on your online application, get their approximate location from their IP address using the IPGeolocation.io API.
  2. Check if the viewed page or content on it is in restricted zone(s).
  3. Check if the user's location is in any restricted zone.
  4. If the page contains content restricted in the user's location, remove the content from the page or display a blocked message.

Here is how you can do it using IPGeolocation.io API:

Before moving on to code, you need to get an API key to use IPGeolocation.io API. If you do not have an account at IPGeolocation.io, then:


Geo-Block Your Visitors using IPGeolocation.io jQuery SDK

Open a text editor and open the page, with restricted access, of your website, say index.html.

Add the following script in the head tag of the page.

<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.1.2/ipgeolocation.min.js"></script>

Add another script block at the bottom of the head tag of the page.

<script>
  // Enable sessionStorage usage to store API response on client-side. This avoids duplicate API calls for a visitor visiting multiple pages during a single visit.
  _ipgeolocation.enableSessionStorage(true);

  // Disable async calls to ipgeolocation.io API, so the check finishes before the rest of the page loads.
  _ipgeolocation.makeAsyncCallsToAPI(false);

  // Fetch only the `country_code2` field from the response excluding rest of the response as we'll restrict access based on the country.
  _ipgeolocation.setFields("country_code2");

  // Get IP-Location for the visitor's IP address. Replace "YOUR_API_KEY" with the API key from the ipgeolocation.io dashboard.
  _ipgeolocation.getGeolocation(redirectToUnauthorizedPage, "YOUR_API_KEY");

  function redirectToUnauthorizedPage(response) {
    // Allow visitors only from US or CA: leave them on the page they requested.
    if (response && ["US", "CA"].includes(response.country_code2)) return;

    // Send everyone else, including failed lookups, to the error page unless they're already on it.
    if (window.location.pathname !== "/unauthorized-access.html") {
      window.location.replace("https://site.com/unauthorized-access.html");
    }
  }
</script>

This browser example controls navigation only. Enforce access restrictions on the server or edge before sending protected content.


Geo-Blocking the Visitors using PHP

Open a text editor and open the page of your website with restricted access, say index.php.

Add the following code at the very top of index.php, before any HTML output. PHP can only send a redirect header before the page starts rendering.

<?php
  // query ipgeolocation.io API and returns JSON response
  function get_geolocation($apiKey, $ip, $lang = "en", $fields = "*") {
    $url = "https://api.ipgeolocation.io/ipgeo?apiKey=".$apiKey."&ip=".$ip."&lang=".$lang."&fields=".$fields;
    $cURL = curl_init();

    curl_setopt($cURL, CURLOPT_URL, $url);
    curl_setopt($cURL, CURLOPT_HTTPGET, true);
    curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cURL, CURLOPT_HTTPHEADER, array (
      "Accept: application/json"
    ));

    return curl_exec($cURL);
  }

  // get ipgeolocation for the visitor's IP address. Replace YOUR_API_KEY with your API key.
  $json = get_geolocation("YOUR_API_KEY", $_SERVER["REMOTE_ADDR"], "en", "country_code2");
  $geolocation = json_decode($json, true);
  $currentWebsite = $_SERVER["SERVER_NAME"];
  $redirect = false;
  $redirectTo = null;

  // allow visitors only from US or CA, else redirect to security error message
  if ($geolocation["country_code2"] == "US" || $geolocation["country_code2"] == "CA") {
    $redirect = false;
  } else {
    $redirect = true;
    $redirectTo = "https://site.com/unauthorized-access";
  }

  if ($redirect) {
    header("Location: ".$redirectTo);
    die();
  }
?>

Related Articles

VPN Detection False Positives: Stop Fraud, Not Customers
VPN Detection False Positives: Stop Fraud, Not Customers

A VPN flag can be right about the address and still wrong about the customer. See where VPN detection false positives come from, the context that separates a privacy-minded customer from a fraudster, and how to measure the good users your rules turn away.

Posted on November 4, 2026
Read More: VPN Detection False Positives: Stop Fraud, Not Customers
How Should You Use an IP Threat Score?
How Should You Use an IP Threat Score?

Set IP threat score rules per action, learn which flags should change the answer, and test it all in shadow mode. IPGeolocation.io returns one of the most detailed security responses in one call: 27 fields behind every score.

Posted on October 23, 2026
Read More: How Should You Use an IP Threat Score?
Residential Proxy Detection: How It Works, Why It's Hard
Residential Proxy Detection: How It Works, Why It's Hard

Residential proxies make automated traffic look like real households, which is exactly why IP reputation checks miss them. Here is how detection actually works: provider attribution, network and behavior signals, live connection analysis, and confidence scores instead of binary blocks.

Posted on July 27, 2026
Read More: Residential Proxy Detection: How It Works, Why It's Hard
What Is VPN Detection and How Does It Work?
What Is VPN Detection and How Does It Work?

VPN detection identifies traffic routed through VPN servers before it reaches your app. Here is how the two signal families work, what they miss, and what to do with a positive result.

Posted on July 27, 2026
Read More: What Is VPN Detection and How Does It Work?
iCloud Private Relay vs Cloudflare WARP, Explained
iCloud Private Relay vs Cloudflare WARP, Explained

iCloud Private Relay and Cloudflare WARP both hide a visitor's real IP address, but they are built for different jobs and hide it in different ways. Private Relay protects Safari browsing, DNS queries, and unencrypted HTTP app traffic through a two-hop relay system. WARP is a whole-device tunnel through Cloudflare's network. Both keep the user near their real region, and, as you will see below, our detection API flags both as relays. If you run a website, an app, or a fraud pipeline, you meet b

Posted on July 23, 2026
Read More: iCloud Private Relay vs Cloudflare WARP, Explained
IP Geolocation Data: Built, Not Guessed
IP Geolocation Data: Built, Not Guessed

A lot of weak "IP geolocation data" is just a WHOIS guess dressed up with marketing. Here is how we actually build ours, layer by layer, from registry data and geofeeds to active measurement and verification, and how accurate it really is.

Posted on July 13, 2026
By Mudassar Tariq
Read More: IP Geolocation Data: Built, Not Guessed

Subscribe to Our Newsletter

Get the latest in geolocation tech, straight to your inbox.