Support Ticket Why UserCV? [FAQ] Knowledgebase Personal Blogging Platform Start Teaching Online
Company is going under Migratiiion - Some of the functionalities may not be available for the time being.
Find website visitors country location using Cloudflare FREE service

Cloudflare is great! We must have to admit it to it. What Cloudflare has done to the internet is equally appreciatable as another big technology like facebook and google has done in the last 1-2 decades.

Well, quite some of the time, it becomes vital to know the user's location to perform some action related to his/her visitor's country, like, "showing up cookie policy for EU visitors", "showing different price based on user's country currency", yeah and there are some more reasons, but we won't dig into that.

We will here learn, how can we utilize Cloudflare technology to know the user's location and do the action based on it.

If you have CMS or App coded in "PHP ", your best bet is to use header information sent by Cloudflare and get the user's country location from it. Just a line of code and it gets your job done. 

Note: Cloudflare only shares 2 character country ISO code, like the US  for the USA, IN for India, AU for Australia and other, you can google the 2 digit country code.

<?php $country = $_SERVER['HTTP_CF_IPCOUNTRY']; ?>

your $country has now a country code of your visitors and now, you can do the rest of the logic "switch" or "if else if else" to do your rest of the magic.

Well, that was easy, right?

But, what if your CMS or App is not in PHP language, you can still get the information via header!

But what! what If I don't have server access and my CMS or App is built upon SAAS product like "", "", "Godaddy web builder" or other related products?

Well, here is country detecting code solution for SAAS platform like "", "", "Godaddy web builder" or any other related platforms!

Javascript code to detect country location for SAAS platform or for without server access platform:-

<script type="text/javascript">

jQuery.ajax('/cdn-cgi/trace', {
    success: function(response) {
        var lines = response.split('\n');
        var key;
        var value;
        lines.forEach(function(line) {
            key = line.split('=');
            value = decodeURIComponent(key[1] || '');
            if (key[0] === 'loc' && value == 'IN') {
                alert('You are From India - Let\'s do the other action');
            } else {
               alert('You don\'t seems to be from India');
        return true;
    error: function() {
        console.log("location tracing failed");


Let's understand, what is happening above in below points.

  1. We are first trying to run "Javascript Ajax" code to fetch the information from Cloudflare path of the website which is running on Cloudflare CDN
  2. After finding the information, we are splitting the response in each line
  3. We are checking the value of "loc" as 'loc" contains the user's 2 character country code.
  4. After fetching the value, we are doing little if-else logic to do our magic things.

That is all. Well, hope you find this code useful in your project, we wish you all the best for your job and for any request, you need to get done! you can definitely hire some of the best freelancers in our Marketplace.

Tags: Cloudflare location tracker detect visitors location wix find visitors location shopify find visitors country

About the Author