Geo-Block the Visitors to Your Online Applications

Sheharyar Malik
By Sheharyar Malik 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

Blog-Article
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 on January 8, 2026
Read More
Blog-Article
A Practical Guide to IPGeolocation.io’s Astronomy API

Explanation of IPGeolocation.io's Astronomy API, presenting its use cases and fields' practical interpretation.

Posted on October 15, 2025
Read More
Blog-Article
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 on August 25, 2025
Read More
Blog-Article
Protecting Your Business from VPN & Proxy Fraud: A Comprehensive Guide

Prevent online payment fraud, account takeovers, bot attacks, and other malicious activities. Detect and block VPNs to secure customers.

Posted on August 24, 2025
Read More
Blog-Article
5 Companies Employing IP Geolocation for Success (What We Can Learn)

Learn how top brands like Netflix, Amazon, and others succeed with IP geolocation to localize content, improve ads, and secure users, and how you can, too.

Posted on June 30, 2025
Read More
Blog-Article
How IP Geolocation Powers Everyday Apps: 5 Surprising Ways It Improves Your Online Experience

Learn how businesses use IP Geolocation to personalize content, enhance security, ensure regulatory compliance, and optimize performance in modern web applications

Posted on June 3, 2025
Read More

Subscribe to Our Newsletter

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