欢迎来到素材无忧网,按 + 收藏我们
登录 注册 退出 找回密码

js用php+ajax利用淘宝IP库获取访客ip和所在地

时间: 2020-09-15 15:34 阅读: 作者:素材无忧网

JS异步请求自己站的ip/index.php接口,而自己站接口通过PHP在淘宝API上获取归属地查询


<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>ip2</title>
<script type="text/javascript">
function showJson(){
	var test;
	if(window.XMLHttpRequest){
		test = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		test = new window.ActiveXObject();
	}else{
		alert("请升级至最新版本的浏览器");
	}
	if(test !=null){
		test.open("GET",location.protocol+'//'+location.hostname+'/ip/index.php',true);
		test.send(null);
		test.onreadystatechange=function(){
			if(test.readyState==4&&test.status==200){
				var data = JSON.parse(test.responseText);
				document.getElementById("address").innerHTML = "IP:" + data.data.ip+data.data.region+data.data.city+'['+data.data.isp+']';
			}
		};
	 
	}
}
window.onload=function(){
  showJson();
};
</script>
</head>
<body>
<center id="address"></center>
</body>
</html>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>ip</title>
<script type="text/javascript" src="/uploads/allimg/200915/1534543445-0.jpg"></script>
<script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        type : 'get',
        url : location.protocol+'//'+location.hostname+'/ip/index.php',
        dataType : 'json',
        success : function(data) {
            if (data.code==0) {
                var str = '';
                if (data.data.country_id=='CN') {
                    str += data.data.ip+data.data.region+data.data.city+'['+data.data.isp+']';
                } else {
                    str += data.data.country+'&nbsp;'+data.data.area;
                }
                $('#address').text("IP:"+str)
            } else {
                $('#address').text('(没查到)');
            }
        },
        error : function(data) {
            console.log(data);
        }
    });
});

</script>
</head>
<body>
<center id="address"></center>
</body>
</html>

PHP代码-/ip/index.php-放在自己的网站里

<?php
function getip(){
	if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
		$ip = getenv('HTTP_CLIENT_IP');
	} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
		$ip = getenv('HTTP_X_FORWARDED_FOR');
	} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
		$ip = getenv('REMOTE_ADDR');
	} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '127.0.0.1';
}
$ip = getip();
$ipjson = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip);
echo $ipjson;
die();

 

版权声明: 本站资源均来自互联网或会员发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!

转载请注明: js用php+ajax利用淘宝IP库获取访客ip和所在地

模板推荐