php查询ip所在地的方法__2

上传人:s9****2 文档编号:472759670 上传时间:2023-10-16 格式:DOCX 页数:13 大小:14.07KB
返回 下载 相关 举报
php查询ip所在地的方法__2_第1页
第1页 / 共13页
php查询ip所在地的方法__2_第2页
第2页 / 共13页
php查询ip所在地的方法__2_第3页
第3页 / 共13页
php查询ip所在地的方法__2_第4页
第4页 / 共13页
php查询ip所在地的方法__2_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《php查询ip所在地的方法__2》由会员分享,可在线阅读,更多相关《php查询ip所在地的方法__2(13页珍藏版)》请在金锄头文库上搜索。

1、php查询ip所在地的方法_ 具体实现方法如下: 代码如下: ?php /* * date 2021.12.21 注:文件头 第一条索引的偏移量 (4byte) + 最终一条索引的偏移地址 (4byte) 8字节 记录区 结束ip (4byte) + 地区1 + 地区2 4字节+不定长 索引区 开头ip (4byte) + 指向记录区的偏移地址 (3byte) 7字节 */ class iplocation var $fp; var $firstip; /第一条ip索引的偏移地址 var $lastip; /最终一条ip索引的偏移地址 var $totalip; /总ip数 /* |- | 构

2、造函数,初始化一些变量 |- | */ function iplocation($datfile = wry.dat) $this-fp=fopen($datfile,rb)or die(wry.dat不存在,请去网上 a href=l下载纯真ip数据 库/a, wry.dat 放到当前名目下); /二制方式打开 $this-firstip = $this-get4b(); /第一条ip索引的肯定偏移地址 $this-lastip = $this-get4b(); /最终一条ip索引的肯定偏移地址 $this-totalip =($this-lastip - $this-firstip)/7

3、; /ip总数 索引区是定长的7个字节,在此要除以7, register_shutdown_function(array($this,closefp); /为了兼容php5以下版本,本类没有用析构函数,自动关闭ip库. /* |- | 关闭ip库 |- | */ function closefp() fclose($this-fp); /* |- | 读取4个字节并将解压成long的长模式 |- | */ function get4b() $str=unpack(v,fread($this-fp,4); return $str1; /* |- | 读取重定向了的偏移地址 |- | */ fun

4、ction getoffset() $str=unpack(v,fread($this-fp,3).chr(0); return $str1; /* |- | 读取ip的具体地址信息 |- | */ function getstr() $split=fread($this-fp,1); while (ord($split)!=0) $str .=$split; $split=fread($this-fp,1); return $str; /* |- | 将ip通过ip2long转成ipv4的互联网地址,再将他压缩成big-endian字节序 ,用来和索引区内的ip地址做比较 |- | */ f

5、unction iptoint($ip) return pack(n,intval(ip2long($ip); /* |- | 猎取地址信息 |- | */ function readaddress() $now_offset=ftell($this-fp); /得到当前的指针位址 $flag=$this-getflag(); switch (ord($flag) case 0: $address=; break; case 1: case 2: fseek($this-fp,$this-getoffset(); $address=$this-getstr(); break; default:

6、 fseek($this-fp,$now_offset); $address=$this-getstr(); break; return $address; /* |- | 猎取标记1或2 用来确定地址是否重定向了 |- | */ function getflag() return fread($this-fp,1); /* |- | 用二分查找法在索引区内搜索ip |- | */ function searchip($ip) $ip=gethostbyname($ip); /将域名转成ip $ip_offsetip=$ip; $ip=$this-iptoint($ip); /将ip转换成长整

7、型 $firstip=0; /搜索的上边界 $lastip=$this-totalip; /搜索的下边界 $ipoffset=$this-lastip; /初始化为最终一条ip地址的偏移地址 while ($firstip = $lastip) $i=floor($firstip + $lastip) / 2); /计算近似中间记录 floor函数记算给定浮点数小的最大整数,说白了就是四舍五也舍 fseek($this-fp,$this-firstip + $i * 7); /定位指针到中间记录 $startip=strrev(fread($this-fp,4); /读取当前索引区内的开头ip

8、地址,并将其little-endian的字节序转换成big-endian的字节序 if ($ip $startip) $lastip=$i - 1; else fseek($this-fp,$this-getoffset(); $endip=strrev(fread($this-fp,4); if ($ip $endip) $firstip=$i + 1; else $ip_offsetoffset=$this-firstip + $i * 7; break; return $ip_offset; /* |- | 猎取ip地址具体信息 |- | */ function getaddress($

9、ip) $ip_offset=$this-searchip($ip); /猎取ip 在索引区内的肯定编移地址 $ipoffset=$ip_offsetoffset; $addressip=$ip_offsetip; fseek($this-fp,$ipoffset); /定位到索引区 $addressstartip=long2ip($this-get4b(); /索引区内的开头ip 地址 $address_offset=$this-getoffset(); /猎取索引区内ip在ip记录区内的偏移地址 fseek($this-fp,$address_offset); /定位到记录区内 $addressendip=long2ip($this-

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 工作计划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号