Using Cloudflare IP Geolocation to Detect a Users Country

Last Updated Mar 17, 2022
Emma Jagger

Engineer, maker, Google alumna, CMU grad

Table of Contents:

Knowing the user’s geolocation is very important for your business. It helps you to create personalized ads and recommendations based on the customer’s visits and local searches. Also, it helps you to perform security hardening. So, you can block the people of an entire country from visiting your site. Besides, you can perform country-specific directs. For example, you can send British customers to your UK site.  

In this post, you will find the way of detecting a user’s country by using Cloudflare IP geolocation. It will enable you to geolocate your site’s traffic effectively. Let’s dive in.

Don't reinvent the wheel.
Abstract's APIs are production-ready now.

Abstract's suite of API's are built to save you time. You don't need to be an expert in email validation, IP geolocation, etc. Just focus on writing code that's actually valuable for your app or business, and we'll handle the rest.

Get started for free

What Does Cloudflare IP Geolocation Do?

Cloudflare IP geolocation captures the country code of your website visitors. So, you can determine their country of origin if you enable IP geolocation. You can use the information for a variety of purposes. For example, you can offer discounts only to the customers of a specific country. 

It is very easy to use. All you have to do is enable the IP geolocation feature in Cloudflare. It will geolocate your website visitors and pass the country code in ISO 3166-1 Alpha 2 format.

How To Detect A Visitor's Country Of Origin With Cloudflare

So, you might be asking yourself, how does Cloudflare geolocate visitors? First, you have to go to the Cloudflare Dashboard. Then you have to find and activate the IP Geolocation option under the Network app. 

IP Geolocation card in Network app, Cloudflare dashboard.

Now, you can start capturing IP geolocation data from analyzed IP addresses.  

Capturing IP Geolocation Data

Cloudflare supports a variety of languages and frameworks, including PHP, Python, C# NodeJS, and .NET. You can use it in your web application logic and capture the IP geolocation data effortlessly. 

How can you get the visitor’s country of origin? The country code value is passed along in the CF-IPCountry request header to the origin web server. You can capture Cloudflare headers into a variable. Later, you can use it to identify the user’s country of origin. Keep in mind that even Cloudflare can't go around the anonymity that comes with users on the Tor network. Here are some examples:

PHP

For web applications or websites written in PHP, you need to use this code to capture the country code using the cf ipcountry header:


$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

Read: How to geolocate an IP address in PHP

Python 

For your Python web application, you need to add this line:


country_code = request.headers.get('cf-ipcountry')

Read: How to geolocate an IP address in Python

NodeJS

For NodeJS applications, you have to use this code:


const country_code = headers['cf-ipcountry'];

C#.Net

For C#.NET web applications, you have to add this line:


string country_code = HttpContext.Current.Request.Headers.Get("cf-ipcountry");

Alternatives to Using Cloudflare IP Geolocation

Is there any easier solution for detecting a user’s country or to find the IP geolocation information? You can try using Abstract’s Geolocation API. It allows you to get geolocation data, including country codes, region, continent, city, and postal code, effortlessly. You just need to make an API request with the API access key and IP address.

Let’s take a look at a practical example. How about detecting the geolocation data of this IP address: 92.184.105.98

Let’s make an API request.


https://ipgeolocation.abstractapi.com/v1/
  ? api_key = YOUR_UNIQUE_API_KEY
  & ip_address = 92.184.105.98

You will get this response:


{
  "ip": 92.184.105.98,
  "city": "Caen",
  "city_geoname_id": 3029241,
  "region": "Normandie",
  "region_iso_code": "FR-NOR",
  "region_geoname_id": 11071621,
  "postal_code": "14949",
  "country": "France",
  "country_code": "FR",
  "country_geoname_id": 3017382,
  "country_is_eu": true,
  "continent": "Europe",
  "continent_code": "EU",
  "continent_geoname_id": 6255148,
  "longitude": 49.242341,
  "latitude": -0.352637,
  "timezone": {
    "name": "Central European Time",
    "abbreviation": "CET",
    "gmt_offset": "+1",
    "current_time": "2020-03-29T07:35:08-07:00",
    "is_dst": true
  },
  "flag": {
    "emoji": "🇫🇷",
    "unicode": "U+1F1EB U+1F1F7",
    "svg": "https://static.abstractapi.com/country-flags/fr_flag.svg",
    "png": "https://static.abstractapi.com/country-flags/fr_flag.png"
  },
  "connection": {
    "autonomous_system_number": "AS3215",
    "autonomous_system_organization": "France Telecom - Orange, FR",
    "connection_type": "wireless",
    "isp_name": "Orange S.A.",
    "organization_name": "Internet OM"
  }
}

As you can see, the value of the country field is set to France. That means the user’s country of origin is France. Also, it has returned several key information, including timezone, continent, and city. By using this data, you can provide a personalized experience to the users effectively. 

Abstract’s IP geolocation API is easier to use and is one of the best IP geolocation APIs. It doesn’t require you to write any code from scratch. Also, you don’t need to activate any feature, like IP Geolocation. You just need to make a request with the API key and the IP address. So, there is no hassle.

Wrapping Up Cloudflare IP Geolocation 

That’s how you use Cloudflare IP geolocation to detect the country of a user. As you can see, the process is very simple. There is no hassle. So, you can utilize it to uncover key insights effortlessly.

Abstract’s geolocation API enables you to detect geolocation data of visitors to your website effortlessly. Try it now for free.

Cloudflare Geolocation FAQs

How accurate is Cloudflare IP Geolocation?

Just like any other IP geolocation service, Cloudflare IP geolocation has some limitations with accuracy. For example, users can use a variety of services, like VPN, to mask their real IP. As a result, it cannot identify the location accurately. Also, it cannot identify cities and regions with 100% accuracy.

If you focus heavily on data accuracy, you can consider using an alternate solution, like Abstract’s IP Geolocation API. It is constantly updated to improve the accuracy of geolocation data. Also, it is easier to use. All you have to do is make a request with the API key and the IP address. You will get all the necessary info, including country data, region, and city, instantly.

How Do I Find The Geolocation of An Ip Address?

You can find the geolocation of an IP address by using Cloudflare IP Geolocation. Alternatively, you can use Abstract’s IP Geolocation API. It allows you to get the geolocation data quickly. Also, it is easier to use.

Does IP Address Show Geolocation?

The IP address can show your geolocation, such as city and area code. However, it cannot reveal your precise location, like your home address.

Geolocate your websites visitors in seconds using Abstract's IP geolocation API.

Get started for free
Geolocate your websites visitors in seconds using Abstract's IP geolocation API.
Get started