最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get visitor's country name for website - Stack Overflow

programmeradmin3浏览0评论

I am currently working on my website which have display various places according to visitor's country.

i also tried to this examples but i want to get IP address autometically

here is example code

HTML

<input type="text" id="ip" />
<input type="button" value="Get country of IP" id="submit" />

JavaScript

<script type="text/javascript">
$(function() {
  $("#submit").click(function() {
    if ($('#ip').val()) {
      var ip = $('#ip').val();

      // the request will be to .gp?ip=my.ip.number but we need to avoid the cors issue, and we use cors-anywhere api.

      $.getJSON("://www.geoplugin/json.gp?ip=" + ip, function(response) {
        console.log(response);
        // output an object which contains the information.
        $("#content").html("Hello visitor from " + response.geoplugin_countryName);

      });
    } else {
      alert("Please provide a IP");
    }
  });
});

</script>

i am new in PHP so please guide me.. Thanks :)

I am currently working on my website which have display various places according to visitor's country.

i also tried to this examples but i want to get IP address autometically

here is example code

HTML

<input type="text" id="ip" />
<input type="button" value="Get country of IP" id="submit" />

JavaScript

<script type="text/javascript">
$(function() {
  $("#submit").click(function() {
    if ($('#ip').val()) {
      var ip = $('#ip').val();

      // the request will be to http://www.geoplugin/json.gp?ip=my.ip.number but we need to avoid the cors issue, and we use cors-anywhere api.

      $.getJSON("https://cors-anywhere.herokuapp./http://www.geoplugin/json.gp?ip=" + ip, function(response) {
        console.log(response);
        // output an object which contains the information.
        $("#content").html("Hello visitor from " + response.geoplugin_countryName);

      });
    } else {
      alert("Please provide a IP");
    }
  });
});

</script>

i am new in PHP so please guide me.. Thanks :)

Share Improve this question asked Apr 18, 2017 at 7:29 user7596515user7596515 2
  • 1 why don't you start by checking the geoplugin docs? – Rotimi Commented Apr 18, 2017 at 7:29
  • you can get visitor IP address with country by using GEO IP github./maxmind/geoip-api-php – vinox Commented Apr 18, 2017 at 7:34
Add a ment  | 

1 Answer 1

Reset to default 5

Try the following code

// Jquery
$.getJSON("http://freegeoip/json/", function(data) {
  var ip = data.ip;
  var country = data.country_name;
  $("#ip").val(ip);
  $("#content").html("Hello visitor from " + country);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="ip" />
<input type="button" value="Get country of IP" id="submit" />
<div id="content">
</div>

发布评论

评论列表(0)

  1. 暂无评论