Question

In my prestashop site i want to know the country/country code of my website visitors.If a visitor open my website i want show an alert as from which country he is opening my site.Is there any php or jquery script available to get the country code of a visitor?

Was it helpful?

Solution

Based on user ip address we can show the location

Try this

$.get("http://ipinfo.io", function(response) {
     alert(response.loc);
}, "jsonp");

DEMO

For Country Code,Name

$(window).load(function(){
jQuery.getJSON('http://freegeoip.net/json/', function(location) {
  alert(location.country_code);
  alert(location.country_name);
});
});

DEMO

OTHER TIPS

function getIP(){
    return isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
}

$ip = getIP();

$loc_content = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=YOUR_API_KEY&ip=$ip&format=json");
$loc_content = json_decode($loc_content);

$code = $loc_content->countryCode;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top