分享wordpress主题显示评论者IP的归属地及运营商信息

灵感来源张戈博客phper_的博客-CSDN博客

最近微博、微信、今日头条、抖音、知乎、小红书等平台纷纷上线显示“IP属地”功能,用户在发送内容或发表评论时,都会展示IP属地。按照微博公布的信息,平台会根据用户近期发博、发评论和投票的IP属地来判定账号所属的地区,国内用户将显示到省份/地区,国外用户显示到国家,并且该功能为强制开启,用户无法主动关闭。

博主测试了在线获取和本地数据 2 种方案,都还不错!不过在线获取方式,若是没有做静态化可能会稍微拖慢加载速度。

一、在线方案1(免费次数太少,不建议商用)

张戈博客原在线方案的在线 API:淘宝、百度和新浪的 API 接口都相继关停或失效了,总之已经没法用了。。。

在此介绍一个新的接口:IP查询API接口_免费数据接口 – 极速数据 (jisuapi.com)

该接口提供全国数百万IP的归属地、运营商类型查询,定期更新。免费

  • 免费会员:500次/天
  • 白银会员:1000次/天
  • 钻石会员:15万次/日

只需要将代码添加到 WordPress 主题函数模板文件 functions.php 中并保存

function curlOpen($url, $config = array())
{
    $arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 20, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>false,'isupfile'=>false);
    $arr = array_merge($arr, $config);
    $ch = curl_init();
     
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
    curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
    curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
    curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
    //curl_setopt($ch, CURLOPT_HEADER, true);//获取header
    if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    if($arr['ssl'])
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    if(!empty($arr['cookie']))
    {
        curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
    }
     
    if(!empty($arr['proxy']))
    {
        //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 
        curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
        if(!empty($arr['userpwd']))
        {           
            curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
        }       
    }   
     
    //ip比较特殊,用键值表示
    if(!empty($arr['header']['ip']))
    {
        array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
        unset($arr['header']['ip']);
    }  
    $arr['header'] = array_filter($arr['header']);
     
    if(!empty($arr['header']))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
    }
 
    if ($arr['post'] != false)
    {
        curl_setopt($ch, CURLOPT_POST, true);
        if(is_array($arr['post']) && $arr['isupfile'] === false)
        {
            $post = http_build_query($arr['post']);           
        }
        else
        {
            $post = $arr['post'];
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }   
    $result = curl_exec($ch);
    //var_dump(curl_getinfo($ch));
    curl_close($ch);
 
    return $result;
}
class ip
{
    public function ipQuery($appkey,$ip)
    {
        $url = "https://api.jisuapi.com/ip/location?appkey=$appkey&ip=$ip";
        $result = curlOpen($url, ['ssl'=>true]);
        $jsonarr = json_decode($result, true);
        //exit(var_dump($jsonarr));
 
        if($jsonarr['status'] != 0)
        {
            return $jsonarr['msg'];
        }
 
        $result = $jsonarr['result'];
        return $result['area'].' '.$result['type'];
    }
}
function get_locate($ip) {
    if(empty($ip)) $ip = get_comment_author_IP();
    $appkey = '你的appkey';//你的appkey
    $query = new ip;
    $result = $query->ipQuery($appkey,$ip);
    return $result;
}

然后,在极速数据 (jisuapi.com)注册一个账号,申请一下对应接口(免费申请的)。最后在 WordPress 评论模板函数中合适的位置插入如下代码即可:

<?php if(b2_get_option('mg_wp','ip') != 0){ ?><span class="commentip-from"><?php echo __('来自 ','b2');echo convertip(get_comment_author_ip()); ?></span><?php } ?>

B2主题可添加在wp-content\themes\b2\Modules\Common\Comment.php:268行后面

二、在线方案2(免费版有限制,支持IPv4、IPv6)

利用 ip-api 免费IP地址查询API接口

function get_locate($ip) {
    if(empty($ip)) $ip = get_comment_author_IP();
    $ch = curl_init();  
    $timeout = 5;  
    curl_setopt ($ch, CURLOPT_URL, 'http://ip-api.com/json/'.$ip.'?lang=zh-CN');  
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
    $file_contents = curl_exec($ch);  
    curl_close($ch);  
    $result = json_decode($file_contents,true);
    if ($result['country'] != '中国') {
        return $result['country'];
    } else {
        return $result['country'].'&nbsp;·&nbsp;'.$result['regionName'].'&nbsp;·&nbsp;'.$result['city'].'&nbsp;·&nbsp;'.$result['isp'];
    }
}

三、本地方案

本地方案则是借助 qq 纯真 ip 数据库来查询 IP 的归属地信息,无需在线获取,从而效率更高。当然,本地数据是不会自己更新的,实时准确性肯定比在线的稍微弱一点,不过我们自己手动更新本地 IP 数据库文件就好了。

数据文件

先下载张戈博客整理好的压缩包,解压后得到 ip2c 文件夹,然后上传到 WordPress 主题目录下。

张戈博客整理好的压缩包

提取码:无
解压码:无

上传到主题目录之后,请编辑 WordPress 主题目录下的 functions.php 文件,添加如下代码:

require_once get_stylesheet_directory() . '/ip2c/ip2c.php'; //IP 归属地和运营商查询功能

接着参考上文在线方案,找到主题自定义的评论样式回调函数,然后在合适的位置加入如下代码:

<?php echo convertip(get_comment_author_ip()); ?>

IP 数据库文件更新,前往out0fmemory/qqwry.dat: 自动更新的纯真ip库,每天自动更新 (github.com)下载qqwry_lastest.dat文件覆盖目录内的.dat文件即可

就能在前台评论列表对应位置展示评论者的 IP 归属地和运营商信息了

三种方案效果图:

分享wordpress主题显示评论者IP的归属地及运营商信息

© 版权声明

给TA赞助
共{{data.count}}人
人已赞助
WP教程

B2主题添加特效插件aos.js

2022-4-6 2:20:08

WP教程

基于B2商城系统添加自己的授权机制

2022-6-10 14:17:31

5 条回复 A文章作者 M管理员
  1. LH

    测试下

  2. 权戈

    推荐使用本地方案或在线方案2

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
文章目录