php专区

 首页 > php专区 > PHP应用 > php类库 > php查询ip所在地代码 - php类库

php查询ip所在地代码 - php类库

分享到:
【字体:
导读:
          ?php ***@date2010 12 21*@author王刚*qq com*@qq373882774注:文件头[第一条索引的偏移量(4byte)]+[最后一条索引的偏移地址(4byte)]8字节记录区[结束ip(4byte)]+[地区1]+[地区2]4...

php查询ip所在地代码

  1. /** 
  2. *@ date         2010.12.21 
  3. *@ author       王刚 
  4. *@ email        373882774@qq.com 
  5. *@ qq           373882774 
  6. 注:文件头 [第一条索引的偏移量 (4byte)] + [最后一条索引的偏移地址 (4byte)]     8字节 
  7. 记录区 [结束ip (4byte)] + [地区1] + [地区2]                                4字节+不定长 
  8. 索引区 [开始ip (4byte)] + [指向记录区的偏移地址 (3byte)]                   7字节 
  9. */ 
  10. class iplocation{ 
  11. var $fp
  12. var $firstip;  //第一条ip索引的偏移地址 
  13. var $lastip;   //最后一条ip索引的偏移地址 
  14. var $totalip;  //总ip数 
  15. /* 
  16. |---------------------------------------------------------------------------- 
  17. | 构造函数,初始化一些变量 
  18. |---------------------------------------------------------------------------- 
  19. | 
  20. */ 
  21. function iplocation($datfile = "qqwry.dat"){ 
  22. $this->fp=fopen($datfile,'rb')or die("qqwry.dat不存在,请去网上 下载纯真ip数据 库, 'qqwry.dat' 放到当前目录下");   //二制方式打开 
  23. $this->firstip = $this->get4b(); //第一条ip索引的绝对偏移地址 
  24. $this->lastip = $this->get4b();  //最后一条ip索引的绝对偏移地址 
  25. $this->totalip =($this->lastip - $this->firstip)/7 ; //ip总数 索引区是定长的7个字节,在此要除以7, 
  26. register_shutdown_function(array($this,"closefp"));  //为了兼容php5以下版本,本类没有用析构函数,自动关闭ip库. 
  27. /* 
  28. |---------------------------------------------------------------------------- 
  29. | 关闭ip库 
  30. |---------------------------------------------------------------------------- 
  31. | 
  32. */ 
  33. function closefp(){ 
  34. fclose($this->fp); 
  35. /* 
  36. |---------------------------------------------------------------------------- 
  37. | 读取4个字节并将解压成long的长模式 
  38. |---------------------------------------------------------------------------- 
  39. | 
  40. */ 
  41. function get4b(){ 
  42. $str=unpack("v",fread($this->fp,4)); 
  43. return $str[1]; 
  44. /* 
  45. |---------------------------------------------------------------------------- 
  46. | 读取重定向了的偏移地址 
  47. |---------------------------------------------------------------------------- 
  48. | 
  49. */ 
  50. function getoffset(){ 
  51. $str=unpack("v",fread($this->fp,3).chr(0)); 
  52. return $str[1]; 
  53. /* 
  54. |---------------------------------------------------------------------------- 
  55. | 读取ip的详细地址信息 
  56. |---------------------------------------------------------------------------- 
  57. | 
  58. */ 
  59. function getstr(){ 
  60. $split=fread($this->fp,1); 
  61. while (ord($split)!=0) { 
  62. $str .=$split
  63. $split=fread($this->fp,1); 
  64. return $str
  65. /* 
  66. |---------------------------------------------------------------------------- 
  67. | 将ip通过ip2long转成ipv4的互联网地址,再将他压缩成big-endian字节序 ,用来和索引区内的ip地址做比较 
  68. |---------------------------------------------------------------------------- 
  69. | 
  70. */ 
  71. function iptoint($ip){ 
  72. return pack("n",intval(ip2long($ip))); 
  73. /* 
  74. |---------------------------------------------------------------------------- 
  75. | 获取地址信息 
  76. |---------------------------------------------------------------------------- 
  77. | 
  78. */ 
  79. function readaddress(){ 
  80. $now_offset=ftell($this->fp); //得到当前的指针位址 
  81. $flag=$this->getflag(); 
  82. switch (ord($flag)){ 
  83. case 0: 
  84. $address=""
  85. break
  86. case 1: 
  87. case 2: 
  88. fseek($this->fp,$this->getoffset()); 
  89. $address=$this->getstr(); 
  90. break
  91. default
  92. fseek($this->fp,$now_offset); 
  93. $address=$this->getstr(); 
  94. break
  95. return $address
  96. /* 
  97. |---------------------------------------------------------------------------- 
  98. | 获取标志1或2   用来确定地址是否重定向了 
  99. |---------------------------------------------------------------------------- 
  100. | 
  101. */ 
  102. function getflag(){ 
  103. return fread($this->fp,1); 
  104. /* 
  105. |---------------------------------------------------------------------------- 
  106. | 用二分查找法在索引区内搜索ip 
  107. |---------------------------------------------------------------------------- 
  108. | 
  109. */ 
  110. function searchip($ip){ 
  111. $ip=gethostbyname($ip);     //将域名转成ip 
  112. $ip_offset["ip"]=$ip
  113. $ip=$this->iptoint($ip);    //将ip转换成长整型 
  114. $firstip=0;                 //搜索的上边界 
  115. $lastip=$this->totalip;     //搜索的下边界 
  116. $ipoffset=$this->lastip;    //初始化为最后一条ip地址的偏移地址 
  117. while ($firstip <= $lastip){ 
  118. $i=floor(($firstip + $lastip) / 2);          //计算近似中间记录 floor函数记算给定浮点数小的最大整数,说白了就是四舍五也舍 
  119. fseek($this->fp,$this->firstip + $i * 7);    //定位指针到中间记录 
  120. $startip=strrev(fread($this->fp,4));         //读取当前索引区内的开始ip地址,并将其little-endian的字节序转换成big-endian的字节序 
  121. if ($ip < $startip) { 
  122. $lastip=$i - 1; 
  123. else { 
  124. fseek($this->fp,$this->getoffset()); 
  125. $endip=strrev(fread($this->fp,4)); 
  126. if ($ip > $endip){ 
  127. $firstip=$i + 1; 
  128. else { 
  129. $ip_offset["offset"]=$this->firstip + $i * 7; 
  130. break
  131. return $ip_offset
  132. /* 
  133. |---------------------------------------------------------------------------- 
  134. | 获取ip地址详细信息 
  135. |---------------------------------------------------------------------------- 
  136. | 
  137. */ 
  138. function getaddress($ip){ 
  139. $ip_offset=$this->searchip($ip);  //获取ip 在索引区内的绝对编移地址 
  140. $ipoffset=$ip_offset["offset"]; 
  141. $address["ip"]=$ip_offset["ip"]; 
  142. fseek($this->fp,$ipoffset);      //定位到索引区 
  143. $address["startip"]=long2ip($this->get4b()); //索引区内的开始ip 地址 
  144. $address_offset=$this->getoffset();            //获取索引区内ip在ip记录区内的偏移地址 
  145. fseek($this->fp,$address_offset);            //定位到记录区内 
  146. $address["endip"]=long2ip($this->get4b());   //记录区内的结束ip 地址 
  147. $flag=$this->getflag();                      //读取标志字节 
  148. switch (ord($flag)) { 
  149. case 1:  //地区1地区2都重定向 
  150. $address_offset=$this->getoffset();   //读取重定向地址 
  151. fseek($this->fp,$address_offset);     //定位指针到重定向的地址 
  152. $flag=$this->getflag();               //读取标志字节 
  153. switch (ord($flag)) { 
  154. case 2:  //地区1又一次重定向, 
  155. fseek($this->fp,$this->getoffset()); 
  156. $address["area1"]=$this->getstr(); 
  157. fseek($this->fp,$address_offset+4);      //跳4个字节 
  158. $address["area2"]=$this->readaddress();  //地区2有可能重定向,有可能没有 
  159. break
  160. default//地区1,地区2都没有重定向 
  161. fseek($this->fp,$address_offset);        //定位指针到重定向的地址 
  162. $address["area1"]=$this->getstr(); 
  163. $address["area2"]=$this->readaddress(); 
  164. break
  165. break
  166. case 2: //地区1重定向 地区2没有重定向 
  167. $address1_offset=$this->getoffset();   //读取重定向地址 
  168. fseek($this->fp,$address1_offset);   
  169. $address["area1"]=$this->getstr(); 
  170. fseek($this->fp,$address_offset+8); 
  171. $address["area2"]=$this->readaddress(); 
  172. break
  173. default//地区1地区2都没有重定向 
  174. fseek($this->fp,$address_offset+4); 
  175. $address["area1"]=$this->getstr(); 
  176. $address["area2"]=$this->readaddress(); 
  177. break
  178. //*过滤一些无用数据 
  179. if (strpos($address["area1"],"cz88.net")!=false){ 
  180. $address["area1"]="未知"
  181. if (strpos($address["area2"],"cz88.net")!=false){ 
  182. $address["area2"]=" "
  183. return $address
  184.  
  185. /*用法如下:*/ 
  186. $ip=new iplocation("qqwry.dat"); 
  187. $address=$ip->getaddress("221.231.102.100"); 
  188. //$address=$ip->getaddress(www.111cn.net); 
  189. echo '
    '
  190. print_r($address); 
  191. ?> 
分享到:
PHP统计目录下的文件总数及代码行数 - ph...
PHP统计目录下的文件总数及代码行数
php 两表合并成新表并且有序排列 - php类...
php 两表合并成新表并且有序排列  
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……