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