冰豆网

分享网络精彩
bingdou.com.cn

PHP+Jquery给网站添加访客IP地址和IP归属地显示功能 AJAX无刷新显示IP 淘宝IP库

时间:2022-01-20加入收藏

现在很多网站为了对访客友好,经常会需要显示访客的IP地址和地址归属地信息,但是如果直接使用PHP代码调用,可能会阻塞网站的加载速度,这里使用jquery的无刷新显示技术,在读取完访客信息后再插入页码显示。
获取IP地址及IP归属地,这里使用淘宝的IP数据库
以下PHP代码,存为ip.php

<?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.'"}]';
?>


调用数据方法:
前端页面调用方式(需要引入jquery):

<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>

打 赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

TGA: php 技巧

分享到:


官方微信二维码冰豆网官方微信公众号