首页 > 语言 > PHP > 正文

php查询ip所在地代码

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表