php or jquery script to fetch the visitors country code for my prestashop site [closed]

StackOverflow https://stackoverflow.com/questions/23427036

  •  14-07-2023
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top