Geo-Block the Visitors to Your Online Applications

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

Today, the online applications are most vulnerable facing a lots of cyber attacks and exploits. Some online applications or some of their contents are not available in some regions of the world. You can geo-restrict the visitors from non-availability zones on your application using IP location services like 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 it's IP-Location from ipgeolocation.io API.
  2. Check if the viewed page or content on it is in restricted zone(s).
  3. Check 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 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. This
  _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) {
    country_code2 = response.country_code2;

    // allow visitors only from US or CA, else redirect to security error message

    if (country_code2 === 'US' || country_code2 === 'CA') {
      window.location.href = "https://site.com/";
    } else {
      window.location.href = "https://site.com/unauthorized-access.html";
    }
  }
</script>

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 script in the page.

<?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

What Is a CGI Proxy and Is It Still a Threat?
What Is a CGI Proxy and Is It Still a Threat?

Blog Excerpt: A CGI proxy is an old-school web proxy you use through a web page, and it leaves an obvious trace. The destination sees the proxy server's IP, not the visitor's. Here is how it works, whether it still matters, and how to flag the traffic.

Posted onJune10, 2026
Read More
Benefits of IP Geolocation: 8 Real Business Outcomes
Benefits of IP Geolocation: 8 Real Business Outcomes

Eight benefits of IP geolocation that translate directly into revenue, security, and compliance outcomes, with an industry matrix showing which ones matter most for eCommerce, SaaS, AdTech, Fintech, Streaming, and regulated industries. Primary Keyword: benefits of ip geolocation

Posted onJune9, 2026
Read More
Your VPN Isn't Secret: How We Detect Risky & Hidden IPs
Your VPN Isn't Secret: How We Detect Risky & Hidden IPs

VPN detection is only as good as its data. We monitor 160+ providers, run live honeypots, and update threat scores daily. Most APIs won't tell you this much.

Posted onMay21, 2026
Read More
Top 10 IP Geolocation Use Cases in 2025 for Businesses and Developers
Top 10 IP Geolocation Use Cases in 2025 for Businesses and Developers

Discover the most impactful IP geolocation use cases in 2025 – from personalized content and fraud detection to compliance and threat intelligence.

Posted onApril29, 2026
Read More
From “Best-Effort Guess” to Trustworthy Signal: The Future of IP Geolocation
From “Best-Effort Guess” to Trustworthy Signal: The Future of IP Geolocation

IP geolocation powers localization, CDNs, and fraud defense - but data is often stale or conflicting. Here’s what breaks and how geofeeds can help.

Posted onJanuary15, 2026
Read More
How to Evaluate a VPN and Proxy Detection API in 2026
How to Evaluate a VPN and Proxy Detection API in 2026

A practical framework for comparing VPN and proxy detection APIs with a focus on detection depth, false positives, latency, and developer experience.

Posted onDecember29, 2025
Read More

Subscribe to Our Newsletter

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