时间:2022-01-20加入收藏
<?php
header('Content-type: application/json');
function curlip($getip)
{
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'http://ip.taobao.com/service/getIpInfo.php?ip='.$getip);
curl_setopt($curl, CURLOPT_USERAGENT, 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14 (.NET CLR 3.5.30729)');
curl_setopt($curl, CURLOPT_VERBOSE, false);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_REFERER, 'http://ip.taobao.com');
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_AUTOREFERER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$ipaddr = curl_exec($curl);
$ipaddr = json_decode($ipaddr);
$country = $ipaddr->data->country;
$area = $ipaddr->data->area;
$region = $ipaddr->data->region;
$city = $ipaddr->data->city;
$isp = $ipaddr->data->isp;
if($isp == $city){$isp='';}
if($city == $region){$city='';}
if($region == $area){$region='';}
if($region == $country){$region='';}
if($area == $country){$area='';}
$ipaddr = $country.$area.$region.$city.$isp;
return $ipaddr;
}
function get_real_ip()
{
foreach (array(
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER)) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip);
//会过滤掉保留地址和私有地址段的IP,例如 127.0.0.1会被过滤
//也可以修改成正则验证IP
if ((bool) filter_var($ip, FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4 |
FILTER_FLAG_NO_PRIV_RANGE |
FILTER_FLAG_NO_RES_RANGE)) {
return $ip;
}
}
}
}
return null;
}
$dailiip = $_SERVER['REMOTE_ADDR'];
$realip = get_real_ip();
if($dailiip != $realip){
$ipoutput2 = "您使用代理IP:".$dailiip."(".curlip($dailiip)."),您的真实IP:".$realip."(".curlip($realip).")";
}
else
{
$ipoutput2 = "您的IP:".$realip."(".curlip($realip).")";
}
echo '[{"status":"'.$ipoutput2.'"}]';
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>getJSON获取IP数据</title>
<script src="//cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
#divframe {
border: 1px solid #999;
width: 500px;
margin: 0 auto;
}
.loadTitle {
background: #CCC;
height: 30px;
}
</style>
<script type="text/javascript">
$(function(){
$.getJSON("ip.php",function(data){
var $jsontip = $("#jsonTip");
$jsontip.empty();//清空内容
$.each(data,function(infoIndex,info){
strHtml = info["status"]+"<br>";
})
$jsontip.html(strHtml);//显示处理后的数据
})
})
</script>
</head>
<body>
<div id="divframe">
<div id="jsonTip"> </div>
</div>
</body>
</html>