Emlog 显示评论者 IP 地理信息的两种方法

  • A+
所属分类:Emlog

Emlog 系统存储了评论人的 IP 地址,有的时候,需要显示该 IP 的真实地理地址,可以利用以下两种方法获得。

第一种、是本地获得 IP 地理地址

通过本地查找本地 IP 数据库(纯真版)来获得,IP 数据库可以到http://www.cz88.net/下载,安装后,在安装目录里把 qqwry.dat 文件提取出来,放到 emlog 根目录。 (与 index.php 同在一个目录中即可。)

然后在 module.php 中添加以下这一大串代码:

  1. <?php
  2.  //获取 IP 地理地址
  3. $data = '254.254.254.254';
  4.  class IpLocation {
  5.       var $fp;
  6.       var $firstip;
  7.       var $lastip;
  8.       var $totalip;
  9.       function getlong() {
  10.          $result = unpack('Vlong', fread($this->fp, 4));
  11.          return $result['long'];
  12.       }
  13.      function getlong3() {
  14.          $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  15.          return $result['long'];
  16.       }
  17.       function packip($ip) {
  18.          return pack('N', intval(ip2long($ip)));
  19.       }
  20.       function getstring($data = "") {
  21.          $char = fread($this->fp, 1);
  22.          while (ord($char) > 0) {
  23.              $data .= $char;
  24.              $char = fread($this->fp, 1);
  25.          }
  26.          return $data;
  27.       }
  28.       function getarea() {
  29.          $byte = fread($this->fp, 1);
  30.          switch (ord($byte)) {
  31.              case 0:
  32.                 $area = "";
  33.                 break;
  34.              case 1:
  35.              case 2:
  36.                 fseek($this->fp, $this->getlong3());
  37.                 $area = $this->getstring();
  38.                 break;
  39.              default:
  40.                 $area = $this->getstring($byte);
  41.                 break;
  42.          }
  43.          return $area;
  44.          }
  45.       function getlocation($ip) {
  46.          if (!$this->fp) return null;
  47.          $location['ip'] = gethostbyname($ip);
  48.          $ip = $this->packip($location['ip']);
  49.          $l = 0;
  50.          $u = $this->totalip;
  51.          $findip = $this->lastip;
  52.          while ($l <= $u) {
  53.              $i = floor(($l + $u) / 2);
  54.              fseek($this->fp, $this->firstip + $i * 7);
  55.              $beginip = strrev(fread($this->fp, 4));
  56.              if ($ip < $beginip) {
  57.                 $u = $i - 1;
  58.              }
  59.              else {
  60.                 fseek($this->fp, $this->getlong3());
  61.                 $endip = strrev(fread($this->fp, 4));
  62.                 if ($ip > $endip) {
  63.                     $l = $i + 1;
  64.                 }
  65.                 else {
  66.                     $findip = $this->firstip + $i * 7;
  67.                     break;
  68.                 }
  69.              }
  70.          }
  71.          fseek($this->fp, $findip);
  72.          $location['beginip'] = long2ip($this->getlong());
  73.          $offset = $this->getlong3();
  74.          fseek($this->fp, $offset);
  75.          $location['endip'] = long2ip($this->getlong());
  76.          $byte = fread($this->fp, 1);
  77.          switch (ord($byte)) {
  78.              case 1:
  79.                 $countryOffset = $this->getlong3();
  80.                 fseek($this->fp, $countryOffset);
  81.                 $byte = fread($this->fp, 1);
  82.                 switch (ord($byte)) {
  83.                     case 2:
  84.                        fseek($this->fp, $this->getlong3());
  85.                        $location['country'] = $this->getstring();
  86.                        fseek($this->fp, $countryOffset + 4);
  87.                        $location['area'] = $this->getarea();
  88.                        break;
  89.                     default:
  90.                        $location['country'] = $this->getstring($byte);
  91.                        $location['area'] = $this->getarea();
  92.                        break;
  93.                 }
  94.                 break;
  95.              case 2:
  96.                 fseek($this->fp, $this->getlong3());
  97.                 $location['country'] = $this->getstring();
  98.                 fseek($this->fp, $offset + 8);
  99.                 $location['area'] = $this->getarea();
  100.                 break;
  101.              default:
  102.                 $location['country'] = $this->getstring($byte);
  103.                 $location['area'] = $this->getarea();
  104.                 break;
  105.          }
  106.          if ($location['country'] == " CZNET") {
  107.              $location['country'] = "未知";
  108.          }
  109.          if ($location['area'] == " CZNET") {
  110.              $location['area'] = "";
  111.          }
  112.          return $location;
  113.       }
  114.       function IpLocation($filename = "qqwry.dat") {
  115.          $this->fp = 0;
  116.          if (($this->fp = @fopen($filename, 'rb')) !== false) {
  117.              $this->firstip = $this->getlong();
  118.              $this->lastip = $this->getlong();
  119.              $this->totalip = ($this->lastip - $this->firstip) / 7;
  120.              register_shutdown_function(array(&$this, '_IpLocation'));
  121.          }
  122.       }
  123.       function _IpLocation() {
  124.          if ($this->fp) {
  125.              fclose($this->fp);
  126.          }
  127.          $this->fp = 0;
  128.       }
  129.  }
  130.  function getaddress($myip){
  131.  $ipOrDomain=$myip;
  132.  $iplocation = new IpLocation();
  133.  $location = $iplocation->getlocation($ipOrDomain);
  134.  $address=mb_convert_encoding($location['country'].$location['area'], "utf-8""gbk");
  135.  return $address;
  136.  }
  137.  ?>

然后在需要显示的地方插入以下代码:

  1. <?php echo getaddress($comment['ip']);?>

是不是就已经成功了呢?

 

第二种、是通过远程网站相关接口实现。

在 module.php 中添加以下代码:

  1. <?php
  2. //blog:获取 IP 地址所在地,提取新浪 IP 接口
  3. function getaddress($ip)
  4. {
  5. //调用 sina 查询接口
  6. $str file_get_contents("http://counter.sina.com.cn/ip ip=".$ip);
  7. //转换字符集
  8. $str = mb_convert_encoding($str,"UTF-8","GBK");
  9. //匹配结果
  10. preg_match_all('/[\x{4e00}-\x{9fa5}]+/u',$str,$get);
  11. //将数组转换成字符串
  12. $add = implode('-',$get[0]);
  13. //返回结果
  14. return $add;
  15. }
  16. ?>

然后在需要显示的地方插入以下代码:

  1. <?php echo getaddress($comment['ip']);?>

图片显示方法:

  1. <a title= <?php echo getaddress($comment['ip']);?>>&nbsp;<img src="./ip.png"></a>

其中 ip.png 这个图标会显示在评论人的后面,鼠标放上去会显示地理地址。

本资源收集于网络,只做学习和交流使用

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: