【发布时间】:2017-09-13 22:31:45
【问题描述】:
我目前正在我的网站上工作,该网站根据访问者的国家/地区显示不同的地方。
我也尝试过这个例子,但我想自动获取 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 新手,所以请指导我.. 谢谢 :)
【问题讨论】:
-
为什么不从检查 geoplugin 文档开始呢?
-
您可以通过使用GEO IP github.com/maxmind/geoip-api-php获取国家访问者IP地址
标签: javascript php jquery html geolocation