b2主题美化:显示评论者ip(IPV6)

Jaysun

文章最后更新时间:2023年03月14日已超过409天没有更新。

现在国家大力提倡网站现IP功能,今天分享一下wordpress主题显IP功能的三种方法,可以根据自己网站的情况进行调整美化。

1.演示效果

11.jpg

2.实现教程

2.1 在线方案一

(免费次数太少,不建议商用)

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

在此介绍一个新的接口:IP查询API接口_免费数据接口 - 极速数据 

该接口提供全国数百万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 echo get_locate(get_comment_author_ip()); ?>

2.2 在线方案二(支持IPV4、IPV6)

利用 ip-api 免费IP地址查询API接口,在function.php添加代码

隐藏内容
评论可见
前往评论

2.3 本地方案(QQ纯真数据库

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

文件下载:https://www.yiluxb.cn/post/118.html

上传到主题目录之后,请编辑 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文件即可:https://github.com/out0fmemory/qqwry.dat

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


您需要 登录账户 后才能发表评论

发表评论

快捷回复: 表情:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog
评论列表 (有 10 条评论,1340人围观)
网友昵称:yy505929
yy505929 V 普通用户 Google Chrome 122.0.0.0 Android 10 10楼
03-27 来自河北 回复
感谢大佬分享!感谢大佬分享!感谢大佬分享!
网友昵称:warlueross
warlueross V 普通用户 Firefox 123.0 Windows 10 x64 9楼
03-08 来自河南 回复
看看准不准确
网友昵称:anlu
anlu V 普通用户 Google Chrome 94.0.4606.71 Windows 10 x64 8楼
01-12 来自河南 回复
knakank看
网友昵称:22222222
22222222 V 普通用户 Google Chrome 117.0.0.0 Windows 10 x64 7楼
2023-09-27 来自浙江 回复
撒比佛挡杀佛表达式京哈
网友昵称:bowyn
bowyn V 普通用户 Google Chrome 113.0.0.0 Mac OS X 10.15.7 6楼
2023-08-21 来自广东 回复
看看准不准确
网友昵称:遇见网络
遇见网络 V 普通用户 Safari 17.0 Apple iPhone 地板
2023-08-03 来自北京 回复
哈哈哈哈哈
网友昵称:ll123456
ll123456 V 普通用户 Google Chrome 114.0.0.0 Windows 10 x64 凉席
2023-07-09 来自上海 回复
[Applause]感谢分享[Applause]
网友昵称:ll123456
ll123456 V 普通用户 Google Chrome 114.0.0.0 Windows 10 x64 板凳
2023-07-09 来自上海 回复
感谢分享
网友昵称:minions
minions V 普通用户 Google Chrome 108.0.5359.95 Windows 10 x64 椅子
2023-06-12 来自江西 回复
感谢分享
网友昵称:Rookie1
Rookie1 V 普通用户 Google Chrome 109.0.0.0 Android 12 沙发
2023-03-28 来自福建 回复
[Applause][Applause][Applause][Applause]文章不错,写的很好!

目录[+]

取消
微信二维码
微信二维码
支付宝二维码