2017-04-18 2 views
1

現在、私は訪問者の国に応じて様々な場所を表示している私のウェブサイトで働いています。ウェブサイトの訪問者の国名を取得する方法

私はまた、この例に試みたが、私はautometically

ここでIPアドレスを取得したいのコード例

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.net/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.com/http://www.geoplugin.net/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> 
です

私は私を導いてくださいPHPに新しいです..

+1

を試してみてくださいなぜあなたはgeopluginのドキュメントを確認することから始めませんか? – Akintunde007

+0

GEO IP https://github.com/maxmind/geoip-api-php – vinox

答えて

3

:)おかげで、次のコード

// Jquery 
 
$.getJSON("http://freegeoip.net/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.com/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

を使用して、国と訪問者のIPアドレスを取得できます。 –

関連する問題