为博客添加ip归属地

搭建IP离线库接口

感谢以下仓库

我将该程序封装到Docker中方便大家调用

https://hub.docker.com/r/mcenjoy/qqwry

搭建方法:

docker run -d --name qqwry -p 2060:2060 mcenjoy/qqwry:latest

这样你就可以通过调用2060端口来获取ip归属地了,当然你可以通过docker-compose.yml来搭建

  qqwry:
    container_name: qqwry
    build: './qqwry'
    restart: always
    deploy:
      resources:
        limits:
          memory: 100M

为 WordPress 添加显示ip归属地功能

在php容器中添加向qqwry容器的连接

  php:
    deploy:
      resources:
        limits:
          memory: 300M
    build: ./php/
    restart: always
    links:
      - mysql
      - qqwry
      - redis
    depends_on:
      - mysql
      - redis
    volumes:
      - ./www:/www
      - ./php/etc/:/opt/bitnami/php/etc/
    container_name: php

在插件或模板中修改

function curl_get_https($url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $tmpInfo = curl_exec($curl);
    curl_close($curl);
    return $tmpInfo;
}

// 添加 ip 归属地显示
function extra_ip_info($c) {
    $ip = get_comment_author_ip();
    $result = curl_get_https("http://qqwry:2060/?ip=".$ip);
    $json=json_decode($result,true);

    $c.='<div class="comment-useragent"><svg t="1661301194348" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1103" width="32" height="32"><path d="M1023.98404 512c0 282.749582-229.216418 512-511.984 512C229.234458 1024 0.00004 794.749582 0.00004 512 0.00004 229.234418 229.234458 0 512.00004 0 794.767622 0 1023.98404 229.234418 1023.98404 512z" fill="#5D9CEC" p-id="1104"></path><path d="M998.890432 353.204481c-24.031625-73.688849-65.404978-142.171779-119.718129-198.046905-54.249152-55.781128-121.438103-99.078452-194.280964-125.218044l-21.281668-7.63988-6.3759 21.71766c-2.937954 9.907845-5.155919 17.063733-6.689896 21.375666-18.999703 8.905861-39.311386 16.937735-58.967078 24.749614-62.71502 24.841612-127.544007 50.54521-172.263309 109.482289-19.455696 25.6256-21.705661 52.391181-5.859908 69.828909 8.29587 9.139857 20.375682 13.781785 35.905439 13.781784 10.123842 0 20.687677-1.90597 29.999531-3.577944 6.561897-1.187981 13.343792-2.405962 17.327729-2.405962 0.295995 0 0.561991 0.016 0.827987 0.031999 22.921642 1.639974 65.624975 9.749848 103.874377 19.717692 42.031343 10.937829 64.312995 20.187685 73.812847 25.579601-6.3759 7.593881-23.467633 19.329698-46.345276 19.329698-11.187825 0-22.061655-2.889955-32.375494-8.579866-15.781753-8.703864-55.281136-16.967735-109.718286-26.687583l-5.281917-0.953985c-2.281964-0.405994-4.889924-0.60999-7.953876-0.609991-22.875643 0-76.8288 11.077827-117.218168 53.031172-35.327448 36.671427-52.045187 87.218637-49.687224 150.217652 1.453977 38.733395 14.593772 71.608881 38.015406 95.044515 23.765629 23.813628 56.875111 36.595428 95.734504 37.063421 4.623928 0.032 9.265855 0.032 13.875783 0.032h3.093952c22.453649 0 45.655287 0 62.811019 6.467899 10.985828 4.123936 24.517617 12.249809 31.829502 38.625396 7.561882 27.437571 9.749848 55.499133 11.999813 85.216669 2.123967 27.967563 4.375932 56.905111 11.49982 85.654661 14.123779 56.93911 43.217325 71.156888 65.122983 73.064859 3.123951 0.249996 6.249902 0.375994 9.311854 0.375994 48.751238 0 81.124732-32.813487 107.594319-63.595007 6.311901-7.345885 13.217793-14.439774 20.531679-21.939657 16.125748-16.531742 32.813487-33.623475 44.689302-55.343135 13.717786-24.999609 18.623709-51.249199 23.437634-76.592803 4.49993-24.061624 8.811862-46.783269 20.249683-66.656959 1.561976-2.687958 3.375947-5.74991 5.375916-9.217856 58.095092-99.610444 72.062874-136.483867 59.783066-157.873533-5.531914-9.59385-15.655755-14.781769-27.469571-13.875783-5.623912 0.437993-11.187825 0.65599-16.499742 0.65599-34.999453 0-63.937001-9.62585-79.404759-26.407588-4.375932-4.749926-6.467899-8.843862-7.405884-11.563819 0.531992-0.032 1.123982-0.045999 1.811971-0.045999 10.24984 0 25.999594 4.203934 41.217356 8.28187 18.405712 4.937923 37.467415 10.031843 54.217153 10.031843 27.283574 0 45.033296-14.171779 48.939235-38.98539 3.875939-7.03189 22.061655-24.171622 34.375463-25.781598l25.499602-3.327948-7.967876-24.431618zM164.297473 611.750441c-16.033749-23.313636-34.391463-45.875283-50.875205-68.828924-15.35976-21.405666-75.310823-118.750145-103.094389-133.717911A514.623959 514.623959 0 0 0 0.00004 512c0 124.624053 44.533304 238.812269 118.532148 327.622881 0.061999 0.061999 0.171997 0.093999 0.327995 0.093998 4.48393 0 46.189278-30.999516 49.969219-34.217465 16.421743-13.937782 30.171529-30.967516 36.327432-51.937188 14.359776-48.811237-13.859783-102.562397-40.859361-141.811785z" fill="#A0D468" p-id="1105"></path></svg> ';
    if (array_key_exists($ip,$json)) {
        if ($json[$ip]["country"] == "") {
            $result = curl_get_https("http://ip-api.com/json/".$ip."?lang=zh-CN");
            $json=json_decode($result,true);

            if ($json["status"]=="success") {
                $c.= " ".$json['country'].$json['regionName'].$json['city']." ";
            }else{
                $c.= " 未知 ";
            }
            
            
        } else {
            $c.= $json[$ip]["country"];
        }
       
    }else{
        $c.= " 未知 ";
    }
    $c.=' </div>';
    return $c;
}
add_filter("argon_comment_ua_icon","extra_ip_info",10,1);

之后你可以查看博客的回复

简单但不推荐的修改

function curl_get_https($url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $tmpInfo = curl_exec($curl);
    curl_close($curl);
    return $tmpInfo;
}

// 添加 ip 归属地显示
function extra_ip_info($c) {
    $c.='<div class="comment-useragent"><svg t="1661301194348" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1103" width="32" height="32"><path d="M1023.98404 512c0 282.749582-229.216418 512-511.984 512C229.234458 1024 0.00004 794.749582 0.00004 512 0.00004 229.234418 229.234458 0 512.00004 0 794.767622 0 1023.98404 229.234418 1023.98404 512z" fill="#5D9CEC" p-id="1104"></path><path d="M998.890432 353.204481c-24.031625-73.688849-65.404978-142.171779-119.718129-198.046905-54.249152-55.781128-121.438103-99.078452-194.280964-125.218044l-21.281668-7.63988-6.3759 21.71766c-2.937954 9.907845-5.155919 17.063733-6.689896 21.375666-18.999703 8.905861-39.311386 16.937735-58.967078 24.749614-62.71502 24.841612-127.544007 50.54521-172.263309 109.482289-19.455696 25.6256-21.705661 52.391181-5.859908 69.828909 8.29587 9.139857 20.375682 13.781785 35.905439 13.781784 10.123842 0 20.687677-1.90597 29.999531-3.577944 6.561897-1.187981 13.343792-2.405962 17.327729-2.405962 0.295995 0 0.561991 0.016 0.827987 0.031999 22.921642 1.639974 65.624975 9.749848 103.874377 19.717692 42.031343 10.937829 64.312995 20.187685 73.812847 25.579601-6.3759 7.593881-23.467633 19.329698-46.345276 19.329698-11.187825 0-22.061655-2.889955-32.375494-8.579866-15.781753-8.703864-55.281136-16.967735-109.718286-26.687583l-5.281917-0.953985c-2.281964-0.405994-4.889924-0.60999-7.953876-0.609991-22.875643 0-76.8288 11.077827-117.218168 53.031172-35.327448 36.671427-52.045187 87.218637-49.687224 150.217652 1.453977 38.733395 14.593772 71.608881 38.015406 95.044515 23.765629 23.813628 56.875111 36.595428 95.734504 37.063421 4.623928 0.032 9.265855 0.032 13.875783 0.032h3.093952c22.453649 0 45.655287 0 62.811019 6.467899 10.985828 4.123936 24.517617 12.249809 31.829502 38.625396 7.561882 27.437571 9.749848 55.499133 11.999813 85.216669 2.123967 27.967563 4.375932 56.905111 11.49982 85.654661 14.123779 56.93911 43.217325 71.156888 65.122983 73.064859 3.123951 0.249996 6.249902 0.375994 9.311854 0.375994 48.751238 0 81.124732-32.813487 107.594319-63.595007 6.311901-7.345885 13.217793-14.439774 20.531679-21.939657 16.125748-16.531742 32.813487-33.623475 44.689302-55.343135 13.717786-24.999609 18.623709-51.249199 23.437634-76.592803 4.49993-24.061624 8.811862-46.783269 20.249683-66.656959 1.561976-2.687958 3.375947-5.74991 5.375916-9.217856 58.095092-99.610444 72.062874-136.483867 59.783066-157.873533-5.531914-9.59385-15.655755-14.781769-27.469571-13.875783-5.623912 0.437993-11.187825 0.65599-16.499742 0.65599-34.999453 0-63.937001-9.62585-79.404759-26.407588-4.375932-4.749926-6.467899-8.843862-7.405884-11.563819 0.531992-0.032 1.123982-0.045999 1.811971-0.045999 10.24984 0 25.999594 4.203934 41.217356 8.28187 18.405712 4.937923 37.467415 10.031843 54.217153 10.031843 27.283574 0 45.033296-14.171779 48.939235-38.98539 3.875939-7.03189 22.061655-24.171622 34.375463-25.781598l25.499602-3.327948-7.967876-24.431618zM164.297473 611.750441c-16.033749-23.313636-34.391463-45.875283-50.875205-68.828924-15.35976-21.405666-75.310823-118.750145-103.094389-133.717911A514.623959 514.623959 0 0 0 0.00004 512c0 124.624053 44.533304 238.812269 118.532148 327.622881 0.061999 0.061999 0.171997 0.093999 0.327995 0.093998 4.48393 0 46.189278-30.999516 49.969219-34.217465 16.421743-13.937782 30.171529-30.967516 36.327432-51.937188 14.359776-48.811237-13.859783-102.562397-40.859361-141.811785z" fill="#A0D468" p-id="1105"></path></svg> ';
 
$ip = get_comment_author_ip();       
$result = curl_get_https("http://ip-api.com/json/".$ip."?lang=zh-CN");
$json=json_decode($result,true);

if ($json["status"]=="success") {
    $c.= " ".$json['country'].$json['regionName'].$json['city']." ";
}else{
    $c.= " 未知 ";
}
            
            
  
    $c.=' </div>';
    return $c;
}
add_filter("argon_comment_ua_icon","extra_ip_info",10,1);

直接全部从ip-api接口获取,但是因为接口是别人的不能保证并发,每次都能获取成功

FAQ

  • argon_comment_ua_icon 是什么?
    argon_comment_ua_icon 是主题中的钩子,每个主题都不太一样,你需要自己去查一下源码,看看主题作者是否留有这些,或者你直接改主题文件。
  • 有些地址不显示是怎么回事?
    目前已经支持ipv6了
  • 有其他问题可以留言!
广告 广告位招租

评论

  1. Windows Chrome
    广东省广州市
    3月前
    2022-9-03 13:11:53

    方法可行 不过感觉评论一多的话会导致网页打开延迟一点

    • Avatar photo
      enjoy
      博主
      Bensz
      Windows Chrome
      河北省保定市
      3月前
      2022-9-04 9:30:17

      的确,在加载时候会导致加载时间延长,但这样可以让以前的IP也显示属地;改进可以在发评论时将属地记录到数据库,显示直接读取数据库就行,但这样需要把以前的IP属地给处理一下。后面有时间我改进一下 //todo

      • enjoy
        Windows Chrome
        广东省广州市
        3月前
        2022-9-04 9:35:50

        加油,看好你喔!

  2. test
    Windows Chrome
    香港中西區香港
    3月前
    2022-9-02 16:33:28

    十分反对自我审查,加速加速

    • Avatar photo
      enjoy
      博主
      hsia
      Windows Chrome
      河北省石家庄市
      3月前
      2022-8-29 6:30:50

      请查看数据库中的 IP 是否正确 查看图片

  3. 阿狗
    Windows Chrome
    四川省成都市
    3月前
    2022-8-28 23:22:45

    我想问一下,自己按照最后一种方法,修改后能显示IP。但是所有人显示的都是服务器的IP。是什么原因呢?

    • Avatar photo
      enjoy
      博主
      阿狗
      Windows Chrome
      台湾省台北市
      3月前
      2022-8-28 23:38:13

      首先,你的服务器是否使用CDN进行加速,如果使用了CDN,请在 wp-config.php 中添加

      //获取真实ip地址
      if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
      $list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
      $_SERVER['REMOTE_ADDR'] = $list[0];
      }

      if(isset($_SERVER['HTTP_X_REAL_IP'])) {
      $list = explode(',',$_SERVER['HTTP_X_REAL_IP']);
      $_SERVER['REMOTE_ADDR'] = $list[0];
      }

      如果你使用了内网穿透服务,请查看相关内外穿透的软件说明,获取真实IP。如果还是不会,请邮件/QQ详谈。

  4. hsia
    Windows Chrome
    四川省成都市
    3月前
    2022-8-27 14:32:29

    试试啊

  5. Guangran
    Windows Chrome
    江西省南昌市
    4月前
    2022-8-24 10:37:29

    我的博客支持显示 IPv6 的归属地,调用的腾讯的API接口。不过腾讯的并发太小了,文字下的留言多,后面的几个就刷不出来了

    • Avatar photo
      enjoy
      博主
      Guangran
      Windows Chrome
      新加坡
      4月前
      2022-8-24 10:56:38

      目前已经支持ipv6了

      • 随意之光
        enjoy
        Android Chrome
        山东省
        4月前
        2022-8-25 15:23:38

        这个离线库中没有IPV6信息吧,是通过ip-api实现的吗

        • Avatar photo
          enjoy
          博主
          随意之光
          Windows Chrome
          河北省石家庄市
          4月前
          2022-8-25 15:26:56

          ipv6是通过访问ip-api进行获取的,而ipv4则是通过自己搭建的接口进行获取。

  6. Macintosh Chrome
    山东省青岛市
    4月前
    2022-8-24 9:29:51

    没必要这么麻烦,我把wp useragent升级了,可以直接用,具体看我的博客链接,你这的评论不让发链接,够严谨的~~

    • Avatar photo
      enjoy
      博主
      obaby
      Windows Chrome
      河北省石家庄市
      4月前
      2022-8-24 9:32:02

      嗯好的,我看看这个插件是用什么接口实现的,可以兼容所有的主题

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
小黄脸
上一篇