搜索
首页后端开发php教程再次提供一个IP地理位置查询类

IP 地理位置查询类
  1. /**
  2. 文件名:IpLocation.class.php
  3. * IP 地理位置查询类 (主文件我上传上来了 还有一个测试文件我上传上来,同时还有一个QQWry.Dat这个大家可以在纯真IP库下载到 因为有6M多 所以这里不上传上来了)
  4. *
  5. * @author 马秉尧
  6. * @version 1.5
  7. * @copyright 2005 CoolCode.CN
  8. */
  9. class IpLocation {
  10. /**
  11. * QQWry.Dat文件指针
  12. * @var resource
  13. */
  14. var $fp;
  15. /**
  16. * 第一条IP记录的偏移地址
  17. * @var int
  18. */
  19. var $firstip;
  20. /**
  21. * 最后一条IP记录的偏移地址
  22. * @var int
  23. */
  24. var $lastip;
  25. /**
  26. * IP记录的总条数(不包含版本信息记录)
  27. * @var int
  28. */
  29. var $totalip;
  30. /**
  31. * 构造函数,打开 QQWry.Dat 文件并初始化类中的信息
  32. * @param string $filename
  33. * @return IpLocation
  34. */
  35. function __construct($filename = "QQWry.Dat") {
  36. $this->fp = 0;
  37. if (($this->fp = @fopen($filename, 'rb')) !== false) {
  38. $this->firstip = $this->getlong();
  39. $this->lastip = $this->getlong();
  40. $this->totalip = ($this->lastip - $this->firstip) / 7;
  41. //注册析构函数,使其在程序执行结束时执行
  42. register_shutdown_function(array(&$this, '__construct'));
  43. }
  44. }
  45. /**
  46. * 返回读取的长整型数
  47. * @access private
  48. * @return int
  49. */
  50. function getlong() {
  51. //将读取的little-endian编码的4个字节转化为长整型数
  52. $result = unpack('Vlong', fread($this->fp, 4));
  53. return $result['long'];
  54. }
  55. /**
  56. * 返回读取的3个字节的长整型数
  57. *
  58. * @access private
  59. * @return int
  60. */
  61. function getlong3() {
  62. //将读取的little-endian编码的3个字节转化为长整型数
  63. $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  64. return $result['long'];
  65. }
  66. /**
  67. * 返回压缩后可进行比较的IP地址
  68. *
  69. * @access private
  70. * @param string $ip
  71. * @return string
  72. */
  73. function packip($ip) {
  74. // 将IP地址转化为长整型数,如果在PHP5中,IP地址错误,则返回False,
  75. // 这时intval将Flase转化为整数-1,之后压缩成big-endian编码的字符串
  76. return pack('N', intval(ip2long($ip)));
  77. }
  78. /**
  79. * 返回读取的字符串
  80. *
  81. * @access private
  82. * @param string $data
  83. * @return string
  84. */
  85. function getstring($data = "") {
  86. $char = fread($this->fp, 1);
  87. while (ord($char) > 0) { // 字符串按照C格式保存,以\0结束
  88. $data .= $char; // 将读取的字符连接到给定字符串之后
  89. $char = fread($this->fp, 1);
  90. }
  91. return $data;
  92. }
  93. /**
  94. * 返回地区信息
  95. *
  96. * @access private
  97. * @return string
  98. */
  99. function getarea() {
  100. $byte = fread($this->fp, 1); // 标志字节
  101. switch (ord($byte)) {
  102. case 0: // 没有区域信息
  103. $area = "";
  104. break;
  105. case 1:
  106. case 2: // 标志字节为1或2,表示区域信息被重定向
  107. fseek($this->fp, $this->getlong3());
  108. $area = $this->getstring();
  109. break;
  110. default: // 否则,表示区域信息没有被重定向
  111. $area = $this->getstring($byte);
  112. break;
  113. }
  114. return $area;
  115. }
  116. /**
  117. * 根据所给 IP 地址或域名返回所在地区信息
  118. * @access public
  119. * @param string $ip
  120. * @return array
  121. */
  122. function getlocation($ip) {
  123. if (!$this->fp) return null; // 如果数据文件没有被正确打开,则直接返回空
  124. $location['ip'] = gethostbyname($ip); // 将输入的域名转化为IP地址
  125. $ip = $this->packip($location['ip']); // 将输入的IP地址转化为可比较的IP地址
  126. // 不合法的IP地址会被转化为255.255.255.255
  127. // 对分搜索
  128. $l = 0; // 搜索的下边界
  129. $u = $this->totalip; // 搜索的上边界
  130. $findip = $this->lastip; // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息)
  131. while ($l $i = floor(($l + $u) / 2); // 计算近似中间记录
  132. fseek($this->fp, $this->firstip + $i * 7);
  133. $beginip = strrev(fread($this->fp, 4)); // 获取中间记录的开始IP地址
  134. // strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式
  135. // 以便用于比较,后面相同。
  136. if ($ip $u = $i - 1; // 将搜索的上边界修改为中间记录减一
  137. } else {
  138. fseek($this->fp, $this->getlong3());
  139. $endip = strrev(fread($this->fp, 4)); // 获取中间记录的结束IP地址
  140. if ($ip > $endip) { // 用户的IP大于中间记录的结束IP地址时
  141. $l = $i + 1; // 将搜索的下边界修改为中间记录加一
  142. } else { // 用户的IP在中间记录的IP范围内时
  143. $findip = $this->firstip + $i * 7;
  144. break; // 则表示找到结果,退出循环
  145. }
  146. }
  147. }
  148. //获取查找到的IP地理位置信息
  149. fseek($this->fp, $findip);
  150. $location['beginip'] = long2ip($this->getlong()); // 用户IP所在范围的开始地址
  151. $offset = $this->getlong3();
  152. fseek($this->fp, $offset);
  153. $location['endip'] = long2ip($this->getlong()); // 用户IP所在范围的结束地址
  154. $byte = fread($this->fp, 1); // 标志字节
  155. switch (ord($byte)) {
  156. case 1: // 标志字节为1,表示国家和区域信息都被同时重定向
  157. $countryOffset = $this->getlong3(); // 重定向地址
  158. fseek($this->fp, $countryOffset);
  159. $byte = fread($this->fp, 1); // 标志字节
  160. switch (ord($byte)) {
  161. case 2: // 标志字节为2,表示国家信息又被重定向
  162. fseek($this->fp, $this->getlong3());
  163. $location['country'] = $this->getstring();
  164. fseek($this->fp, $countryOffset + 4);
  165. $location['area'] = $this->getarea();
  166. break;
  167. default: // 否则,表示国家信息没有被重定向
  168. $location['country'] = $this->getstring($byte);
  169. $location['area'] = $this->getarea();
  170. break;
  171. }
  172. break;
  173. case 2: // 标志字节为2,表示国家信息被重定向
  174. fseek($this->fp, $this->getlong3());
  175. $location['country'] = $this->getstring();
  176. fseek($this->fp, $offset + 8);
  177. $location['area'] = $this->getarea();
  178. break;
  179. default: // 否则,表示国家信息没有被重定向
  180. $location['country'] = $this->getstring($byte);
  181. $location['area'] = $this->getarea();
  182. break;
  183. }
  184. if ($location['country'] == " CZ88.NET") { // CZ88.NET表示没有有效信息
  185. $location['country'] = "未知";
  186. }
  187. if ($location['area'] == " CZ88.NET") {
  188. $location['area'] = "";
  189. }
  190. return $location;
  191. }
  192. /**
  193. * 析构函数,用于在页面执行结束后自动关闭打开的文件。
  194. *
  195. */
  196. function __desctruct() {
  197. if ($this->fp) {
  198. fclose($this->fp);
  199. }
  200. $this->fp = 0;
  201. }
  202. }
  203. ?>
复制代码
  1. require_once('IpLocation.class.php');
  2. $ip='127.0.0.1';
  3. $idADDR=new IpLocation();
  4. print_r($idADDR->getlocation($ip));
  5. ?>
复制代码
  1. /**
  2. * IP 地理位置查询类
  3. *
  4. * @author 马秉尧
  5. * @version 1.5
  6. * @copyright 2005 CoolCode.CN
  7. */
  8. class IpLocation {
  9. /**
  10. * QQWry.Dat文件指针
  11. * @var resource
  12. */
  13. var $fp;
  14. /**
  15. * 第一条IP记录的偏移地址
  16. * @var int
  17. */
  18. var $firstip;
  19. /**
  20. * 最后一条IP记录的偏移地址
  21. * @var int
  22. */
  23. var $lastip;
  24. /**
  25. * IP记录的总条数(不包含版本信息记录)
  26. * @var int
  27. */
  28. var $totalip;
  29. /**
  30. * 构造函数,打开 QQWry.Dat 文件并初始化类中的信息
  31. * @param string $filename
  32. * @return IpLocation
  33. */
  34. function __construct($filename = "QQWry.Dat") {
  35. $this->fp = 0;
  36. if (($this->fp = @fopen($filename, 'rb')) !== false) {
  37. $this->firstip = $this->getlong();
  38. $this->lastip = $this->getlong();
  39. $this->totalip = ($this->lastip - $this->firstip) / 7;
  40. //注册析构函数,使其在程序执行结束时执行
  41. register_shutdown_function(array(&$this, '__construct'));
  42. }
  43. }
  44. /**
  45. * 返回读取的长整型数
  46. * @access private
  47. * @return int
  48. */
  49. function getlong() {
  50. //将读取的little-endian编码的4个字节转化为长整型数
  51. $result = unpack('Vlong', fread($this->fp, 4));
  52. return $result['long'];
  53. }
  54. /**
  55. * 返回读取的3个字节的长整型数
  56. *
  57. * @access private
  58. * @return int
  59. */
  60. function getlong3() {
  61. //将读取的little-endian编码的3个字节转化为长整型数
  62. $result = unpack('Vlong', fread($this->fp, 3).chr(0));
  63. return $result['long'];
  64. }
  65. /**
  66. * 返回压缩后可进行比较的IP地址
  67. *
  68. * @access private
  69. * @param string $ip
  70. * @return string
  71. */
  72. function packip($ip) {
  73. // 将IP地址转化为长整型数,如果在PHP5中,IP地址错误,则返回False,
  74. // 这时intval将Flase转化为整数-1,之后压缩成big-endian编码的字符串
  75. return pack('N', intval(ip2long($ip)));
  76. }
  77. /**
  78. * 返回读取的字符串
  79. *
  80. * @access private
  81. * @param string $data
  82. * @return string
  83. */
  84. function getstring($data = "") {
  85. $char = fread($this->fp, 1);
  86. while (ord($char) > 0) { // 字符串按照C格式保存,以\0结束
  87. $data .= $char; // 将读取的字符连接到给定字符串之后
  88. $char = fread($this->fp, 1);
  89. }
  90. return $data;
  91. }
  92. /**
  93. * 返回地区信息
  94. *
  95. * @access private
  96. * @return string
  97. */
  98. function getarea() {
  99. $byte = fread($this->fp, 1); // 标志字节
  100. switch (ord($byte)) {
  101. case 0: // 没有区域信息
  102. $area = "";
  103. break;
  104. case 1:
  105. case 2: // 标志字节为1或2,表示区域信息被重定向
  106. fseek($this->fp, $this->getlong3());
  107. $area = $this->getstring();
  108. break;
  109. default: // 否则,表示区域信息没有被重定向
  110. $area = $this->getstring($byte);
  111. break;
  112. }
  113. return $area;
  114. }
  115. /**
  116. * 根据所给 IP 地址或域名返回所在地区信息
  117. * @access public
  118. * @param string $ip
  119. * @return array
  120. */
  121. function getlocation($ip) {
  122. if (!$this->fp) return null; // 如果数据文件没有被正确打开,则直接返回空
  123. $location['ip'] = gethostbyname($ip); // 将输入的域名转化为IP地址
  124. $ip = $this->packip($location['ip']); // 将输入的IP地址转化为可比较的IP地址
  125. // 不合法的IP地址会被转化为255.255.255.255
  126. // 对分搜索
  127. $l = 0; // 搜索的下边界
  128. $u = $this->totalip; // 搜索的上边界
  129. $findip = $this->lastip; // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息)
  130. while ($l $i = floor(($l + $u) / 2); // 计算近似中间记录
  131. fseek($this->fp, $this->firstip + $i * 7);
  132. $beginip = strrev(fread($this->fp, 4)); // 获取中间记录的开始IP地址
  133. // strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式
  134. // 以便用于比较,后面相同。
  135. if ($ip $u = $i - 1; // 将搜索的上边界修改为中间记录减一
  136. } else {
  137. fseek($this->fp, $this->getlong3());
  138. $endip = strrev(fread($this->fp, 4)); // 获取中间记录的结束IP地址
  139. if ($ip > $endip) { // 用户的IP大于中间记录的结束IP地址时
  140. $l = $i + 1; // 将搜索的下边界修改为中间记录加一
  141. } else { // 用户的IP在中间记录的IP范围内时
  142. $findip = $this->firstip + $i * 7;
  143. break; // 则表示找到结果,退出循环
  144. }
  145. }
  146. }
  147. //获取查找到的IP地理位置信息
  148. fseek($this->fp, $findip);
  149. $location['beginip'] = long2ip($this->getlong()); // 用户IP所在范围的开始地址
  150. $offset = $this->getlong3();
  151. fseek($this->fp, $offset);
  152. $location['endip'] = long2ip($this->getlong()); // 用户IP所在范围的结束地址
  153. $byte = fread($this->fp, 1); // 标志字节
  154. switch (ord($byte)) {
  155. case 1: // 标志字节为1,表示国家和区域信息都被同时重定向
  156. $countryOffset = $this->getlong3(); // 重定向地址
  157. fseek($this->fp, $countryOffset);
  158. $byte = fread($this->fp, 1); // 标志字节
  159. switch (ord($byte)) {
  160. case 2: // 标志字节为2,表示国家信息又被重定向
  161. fseek($this->fp, $this->getlong3());
  162. $location['country'] = $this->getstring();
  163. fseek($this->fp, $countryOffset + 4);
  164. $location['area'] = $this->getarea();
  165. break;
  166. default: // 否则,表示国家信息没有被重定向
  167. $location['country'] = $this->getstring($byte);
  168. $location['area'] = $this->getarea();
  169. break;
  170. }
  171. break;
  172. case 2: // 标志字节为2,表示国家信息被重定向
  173. fseek($this->fp, $this->getlong3());
  174. $location['country'] = $this->getstring();
  175. fseek($this->fp, $offset + 8);
  176. $location['area'] = $this->getarea();
  177. break;
  178. default: // 否则,表示国家信息没有被重定向
  179. $location['country'] = $this->getstring($byte);
  180. $location['area'] = $this->getarea();
  181. break;
  182. }
  183. if ($location['country'] == " CZ88.NET") { // CZ88.NET表示没有有效信息
  184. $location['country'] = "未知";
  185. }
  186. if ($location['area'] == " CZ88.NET") {
  187. $location['area'] = "";
  188. }
  189. return $location;
  190. }
  191. /**
  192. * 析构函数,用于在页面执行结束后自动关闭打开的文件。
  193. *
  194. */
  195. function __desctruct() {
  196. if ($this->fp) {
  197. fclose($this->fp);
  198. }
  199. $this->fp = 0;
  200. }
  201. }
  202. ?>
复制代码


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

如何使PHP应用程序更快如何使PHP应用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能优化清单:立即提高速度PHP性能优化清单:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

PHP依赖注入:提高代码可检验性PHP依赖注入:提高代码可检验性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能优化:数据库查询优化PHP性能优化:数据库查询优化May 12, 2025 am 12:02 AM

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器