Rumah >pembangunan bahagian belakang >tutorial php >php IP获取城市API(纯真IP数据库)

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

WBOY
WBOYasal
2016-07-25 08:55:581828semak imbas
  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 $ipend = implode('', unpack('L', $DataEnd));
  23. if($ipend $ipNum || $ip2num $Middle= intval(($EndNum + $BeginNum) / 2);
  24. //偏移指针到索引位置读取4个字节
  25. fseek($fd, $ipbegin + 7 * $Middle);
  26. $ipData1 = fread($fd, 4);
  27. if(strlen($ipData1) fclose($fd);
  28. return 'System Error';
  29. }
  30. //提取出来的数据转换成长整形,如果数据是负数则加上2的32次幂
  31. $ip1num = implode('', unpack('L', $ipData1));
  32. if($ip1num $ipNum) {
  33. $EndNum = $Middle;
  34. continue;
  35. }
  36. //取完上一个索引后取下一个索引
  37. $DataSeek = fread($fd, 3);
  38. if(strlen($DataSeek) fclose($fd);
  39. return 'System Error';
  40. }
  41. $DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
  42. fseek($fd, $DataSeek);
  43. $ipData2 = fread($fd, 4);
  44. if(strlen($ipData2) fclose($fd);
  45. return 'System Error';
  46. }
  47. $ip2num = implode('', unpack('L', $ipData2));
  48. if($ip2num
  49. //没找到提示未知
  50. if($ip2num if($Middle == $BeginNum) {
  51. fclose($fd);
  52. return 'Unknown';
  53. }
  54. $BeginNum = $Middle;
  55. }
  56. }
  57. //下面的代码读晕了,没读明白,有兴趣的慢慢读
  58. $ipFlag = fread($fd, 1);
  59. if($ipFlag == chr(1)) {
  60. $ipSeek = fread($fd, 3);
  61. if(strlen($ipSeek) fclose($fd);
  62. return 'System Error';
  63. }
  64. $ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
  65. fseek($fd, $ipSeek);
  66. $ipFlag = fread($fd, 1);
  67. }
  68. if($ipFlag == chr(2)) {
  69. $AddrSeek = fread($fd, 3);
  70. if(strlen($AddrSeek) fclose($fd);
  71. return 'System Error';
  72. }
  73. $ipFlag = fread($fd, 1);
  74. if($ipFlag == chr(2)) {
  75. $AddrSeek2 = fread($fd, 3);
  76. if(strlen($AddrSeek2) fclose($fd);
  77. return 'System Error';
  78. }
  79. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  80. fseek($fd, $AddrSeek2);
  81. } else {
  82. fseek($fd, -1, SEEK_CUR);
  83. }
  84. while(($char = fread($fd, 1)) != chr(0))
  85. $ipAddr2 .= $char;
  86. $AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
  87. fseek($fd, $AddrSeek);
  88. while(($char = fread($fd, 1)) != chr(0))
  89. $ipAddr1 .= $char;
  90. } else {
  91. fseek($fd, -1, SEEK_CUR);
  92. while(($char = fread($fd, 1)) != chr(0))
  93. $ipAddr1 .= $char;
  94. $ipFlag = fread($fd, 1);
  95. if($ipFlag == chr(2)) {
  96. $AddrSeek2 = fread($fd, 3);
  97. if(strlen($AddrSeek2) fclose($fd);
  98. return 'System Error';
  99. }
  100. $AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
  101. fseek($fd, $AddrSeek2);
  102. } else {
  103. fseek($fd, -1, SEEK_CUR);
  104. }
  105. while(($char = fread($fd, 1)) != chr(0)){
  106. $ipAddr2 .= $char;
  107. }
  108. }
  109. fclose($fd);
  110. //最后做相应的替换操作后返回结果
  111. if(preg_match('/http/i', $ipAddr2)) {
  112. $ipAddr2 = '';
  113. }
  114. $ipaddr = "$ipAddr1 $ipAddr2";
  115. $ipaddr = preg_replace('/CZ88.Net/is', '', $ipaddr);
  116. $ipaddr = preg_replace('/^s*/is', '', $ipaddr);
  117. $ipaddr = preg_replace('/s*$/is', '', $ipaddr);
  118. if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
  119. $ipaddr = 'Unknown';
  120. }
  121. return $ipaddr;
  122. }
  123. header("Content-type: text/html; charset=utf-8");
  124. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  125. if(isset($_GET['q'])){
  126. $ip = $_GET['q'];
  127. if(filter_var("http://".$ip,FILTER_VALIDATE_URL,FILTER_FLAG_HOST_REQUIRED)){
  128. $ip = gethostbyname($ip);
  129. }
  130. }
  131. echo "$ip,".mb_convert_encoding(convertip($ip),"utf-8","gb2312");
  132. ?>
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn