EmlogPro显示评论者的IP地理位置非插件

Emlog 评论显示IP地理信息的两种方法,可以为你的博客评论区增加已评论者的IP地理位置显现,也不算什么有用的功能,看别的网站都有所以就加上了,还有就是《使用了cdn获取不到真实IP的解决办法》看这篇文章。第一步:把下面代码添加到你模板的module.php文件内: <?php //获...

Emlog 评论显示IP地理信息的两种方法,可以为你的博客评论区增加已评论者的IP地理位置显现,也不算什么有用的功能,看别的网站都有所以就加上了,还有就是《使用了cdn获取不到真实IP的解决办法》看这篇文章。
第一步:把下面代码添加到你模板的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这个图标会显示在评论人的后面,鼠标放上去会显示地理地址。

首页 导航 会员 客服 微信
QQ 微信 邮箱 TOP