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 "wix.com", "shopify.com", "Godaddy web builder" or other related products?
Well, here is country detecting code solution for SAAS platform like "wix.com", "shopify.com", "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");
}
});
</script>
Let's understand, what is happening above in below points.
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
I am Full Stack Programmer and Engineer. I develop web apps, mobile apps at the best price. My skills are listed at below section. I also write sometimes blog about various tech stacks. Feel free to follow me on my social media channels listed below.
From: Jaipur, Rajasthan India - Last Login: 11 months ago.
PHP programming langauge Laravel Codeigniter MySQL Twitter Bootstrap Material Design HTML5 CSS Javascript language jQuery