当前位置:Gxlcms > PHP教程 > phpIP获取城市API(纯真IP数据库)

phpIP获取城市API(纯真IP数据库)

时间:2021-07-01 10:21:17 帮助过:35人阅读

  1. /**
  2. * 根据IP地址取得城市名称 纯真IP数据库
  3. * edit: bbs.it-home.org
  4. */
  5. function convertip($ip) {
  6. //IP数据文件路径
  7. $dat_path = 'QQWry.Dat';
  8. //检查IP地址
  9. if(!filter_var($ip, FILTER_VALIDATE_IP)) {
  10. return 'IP Address Error';
  11. }
  12. //打开IP数据文件
  13. if(!$fd = @fopen($dat_path, 'rb')){
  14. return 'IP date file not exists or access denied';
  15. }
  16. //分解IP进行运算,得出整形数
  17. $ipNum = ip2long($ip);
  18. //获取IP数据索引开始和结束位置
  19. $DataBegin = fread($fd, 4);
  20. $DataEnd = fread($fd, 4);
  21. $ipbegin = implode('', unpack('L', $DataBegin));
  22. if($ipbegin < 0) $ipbegin += pow(2, 32);
  23. $ipend = implode('', unpack('L', $DataEnd));
  24. if($ipend < 0) $ipend += pow(2, 32); $ipAllNum = ($ipend - $ipbegin) / 7 + 1; $BeginNum = 0; $EndNum = $ipAllNum; //使用二分查找法从索引记录中搜索匹配的IP记录 while($ip1num>$ipNum || $ip2num<$ipNum) {
  25. $Middle= intval(($EndNum + $BeginNum) / 2);
  26. //偏移指针到索引位置读取4个字节
  27. fseek($fd, $ipbegin + 7 * $Middle);
  28. $ipData1 = fread($fd, 4);
  29. if(strlen($ipData1) < 4) {
  30. fclose($fd);
  31. return 'System Error';
  32. }
  33. //提取出来的数据转换成长整形,如果数据是负数则加上2的32次幂
  34. $ip1num = implode('', unpack('L', $ipData1));
  35. if($ip1num < 0) $ip1num += pow(2, 32); //提取的长整型数大于我们IP地址则修改结束位置进行下一次循环 if($ip1num > $ipNum) {
  36. $EndNum = $Middle;
  37. continue;
  38. }
  39. //取完上一个索引后取下一个索引
  40. $DataSeek = fread($fd, 3);
  41. if(strlen($DataSeek) < 3) {
  42. fclose($fd);
  43. return 'System Error';
  44. }
  45. $DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
  46. fseek($fd, $DataSeek);
  47. $ipData2 = fread($fd, 4);
  48. if(strlen($ipData2) < 4) {
  49. fclose($fd);
  50. return 'System Error';
  51. }
  52. $ip2num = implode('', unpack('L', $ipData2));
  53. if($ip2num < 0) $ip2num += pow(2, 32);
  54. //没找到提示未知
  55. if($ip2num < $ipNum) {
  56. if($Middle == $BeginNum) {
  57. fclose($fd);
  58. return 'Unknown';
  59. }
  60. $BeginNum = $Middle;
  61. }
  62. }
  63. //下面的代码读晕了,没读明白,有兴趣的慢慢读
  64. $ipFlag = fread($fd, 1);
  65. if($ipFlag == chr(1)) {
  66. $ipSeek = fread($fd, 3);
  67. if(strlen($ipSeek) < 3) {
  68. fclose($fd);
  69. return 'System Error';
  70. }
  71. $ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
  72. fseek($fd, $ipSeek);
  73. $ipFlag = fread($fd, 1);
  74. }
  75. if($ipFlag == chr(2)) {
  76. $AddrSeek = fread($fd, 3);
  77. if(strlen($AddrSeek) < 3) {
  78. fclose($fd);
  79. return 'System Error';
  80. }
  81. $ipFlag = fread($fd, 1);
  82. if($ipFlag == chr(2)) {
  83. $AddrSeek2 = fread($fd, 3);
  84. if(strlen($AddrSeek2) < 3) {
  85. fclose($fd);
  86. return 'System Error';
  87. }
  88. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  89. fseek($fd, $AddrSeek2);
  90. } else {
  91. fseek($fd, -1, SEEK_CUR);
  92. }
  93. while(($char = fread($fd, 1)) != chr(0))
  94. $ipAddr2 .= $char;
  95. $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
  96. fseek($fd, $AddrSeek);
  97. while(($char = fread($fd, 1)) != chr(0))
  98. $ipAddr1 .= $char;
  99. } else {
  100. fseek($fd, -1, SEEK_CUR);
  101. while(($char = fread($fd, 1)) != chr(0))
  102. $ipAddr1 .= $char;
  103. $ipFlag = fread($fd, 1);
  104. if($ipFlag == chr(2)) {
  105. $AddrSeek2 = fread($fd, 3);
  106. if(strlen($AddrSeek2) < 3) {
  107. fclose($fd);
  108. return 'System Error';
  109. }
  110. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  111. fseek($fd, $AddrSeek2);
  112. } else {
  113. fseek($fd, -1, SEEK_CUR);
  114. }
  115. while(($char = fread($fd, 1)) != chr(0)){
  116. $ipAddr2 .= $char;
  117. }
  118. }
  119. fclose($fd);
  120. //最后做相应的替换操作后返回结果
  121. if(preg_match('/http/i', $ipAddr2)) {
  122. $ipAddr2 = '';
  123. }
  124. $ipaddr = "$ipAddr1 $ipAddr2";
  125. $ipaddr = preg_replace('/CZ88.Net/is', '', $ipaddr);
  126. $ipaddr = preg_replace('/^s*/is', '', $ipaddr);
  127. $ipaddr = preg_replace('/s*$/is', '', $ipaddr);
  128. if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
  129. $ipaddr = 'Unknown';
  130. }
  131. return $ipaddr;
  132. }
  133. header("Content-type: text/html; charset=utf-8");
  134. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  135. if(isset($_GET['q'])){
  136. $ip = $_GET['q'];
  137. if(filter_var("http://".$ip,FILTER_VALIDATE_URL,FILTER_FLAG_HOST_REQUIRED)){
  138. $ip = gethostbyname($ip);
  139. }
  140. }
  141. echo "$ip,".mb_convert_encoding(convertip($ip),"utf-8","gb2312");
  142. ?>

人气教程排行