首頁 >php教程 >php手册 >php读取纯真ip数据库的例子

php读取纯真ip数据库的例子

WBOY
WBOY原創
2016-06-13 09:39:351016瀏覽

代码:

<?<span php
</span><span /*</span><span --------------------------------------------------
ip2address [qqwry.dat]
--------------------------------------------------</span><span */</span>
<span class</span><span  ip {
</span><span var</span> <span $fh</span>; <span //</span><span IP数据库文件句柄</span>
<span var</span> <span $first</span>; <span //</span><span 第一条索引</span>
<span var</span> <span $last</span>; <span //</span><span 最后一条索引</span>
<span var</span> <span $total</span>; <span //</span><span 索引总数
//构造函数</span>
<span function</span><span  __construct() {
</span><span $this</span>->fh = <span fopen</span>('qqwry.dat', 'rb'); <span //</span><span qqwry.dat文件</span>
<span $this</span>->first = <span $this</span>-><span getLong4();
</span><span $this</span>->last = <span $this</span>-><span getLong4();
</span><span $this</span>->total = (<span $this</span>->last - <span $this</span>->first) / 7; <span //</span><span 每条索引7字节</span>
<span }
</span><span //</span><span 检查IP合法性</span>
<span function</span> checkIp(<span $ip</span><span ) {
</span><span $arr</span> = <span explode</span>('.',<span $ip</span><span );
</span><span if</span>(<span count</span>(<span $arr</span>) !=4<span  ) {
</span><span return</span> <span false</span><span ;
} </span><span else</span><span  {
</span><span for</span> (<span $i</span>=0; <span $i</span> < 4; <span $i</span>++<span ) {
</span><span if</span> (<span $arr</span>[<span $i</span>] <'0' || <span $arr</span>[<span $i</span>] > '255'<span ) {
</span><span return</span> <span false</span><span ;
}
} </span><span //</span><span  脚本学堂 www.jbxue.com</span>
<span }
</span><span return</span> <span true</span><span ;
}
</span><span function</span><span  getLong4() {
</span><span //</span><span 读取little-endian编码的4个字节转化为长整型数</span>
<span $result</span> = <span unpack</span>('Vlong', <span fread</span>(<span $this</span>->fh, 4<span ));
</span><span return</span> <span $result</span>['long'<span ];
}
</span><span function</span><span  getLong3() {
</span><span //</span><span 读取little-endian编码的3个字节转化为长整型数</span>
<span $result</span> = <span unpack</span>('Vlong', <span fread</span>(<span $this</span>->fh, 3).<span chr</span>(0<span ));
</span><span return</span> <span $result</span>['long'<span ];
}
</span><span //</span><span 查询信息</span>
<span function</span> getInfo(<span $data</span> = ""<span ) {
</span><span $char</span> = <span fread</span>(<span $this</span>->fh, 1<span );
</span><span while</span> (<span ord</span>(<span $char</span>) != 0) { <span //</span><span 国家地区信息以0结束</span>
<span $data</span> .= <span $char</span><span ;
</span><span $char</span> = <span fread</span>(<span $this</span>->fh, 1<span );
}
</span><span return</span> <span $data</span><span ;
}
</span><span //</span><span 查询地区信息</span>
<span function</span><span  getArea() {
</span><span $byte</span> = <span fread</span>(<span $this</span>->fh, 1); <span //</span><span 标志字节</span>
<span switch</span> (<span ord</span>(<span $byte</span><span )) {
</span><span case</span> 0: <span $area</span> = ''; <span break</span>; <span //</span><span 没有地区信息</span>
<span case</span> 1: <span //</span><span 地区被重定向</span>
<span fseek</span>(<span $this</span>->fh, <span $this</span>-><span getLong3());
</span><span $area</span> = <span $this</span>->getInfo(); <span break</span><span ;
</span><span case</span> 2: <span //</span><span 地区被重定向</span>
<span fseek</span>(<span $this</span>->fh, <span $this</span>-><span getLong3());
</span><span $area</span> = <span $this</span>->getInfo(); <span break</span><span ;
</span><span default</span>: <span $area</span> = <span $this</span>->getInfo(<span $byte</span>); <span break</span>; <span //</span><span 地区没有被重定向</span>
<span }
</span><span return</span> <span $area</span><span ;
}
</span><span function</span> ip2addr(<span $ip</span><span ) {
</span><span if</span>(!<span $this</span> -> checkIp(<span $ip</span><span )){
</span><span return</span> <span false</span><span ;
}
</span><span $ip</span> = <span pack</span>('N', <span intval</span>(<span ip2long</span>(<span $ip</span><span )));
</span><span //</span><span 二分查找</span>
<span $l</span> = 0<span ;
</span><span $r</span> = <span $this</span>-><span total;
</span><span while</span>(<span $l</span> <= <span $r</span><span ) {
</span><span $m</span> = <span floor</span>((<span $l</span> + <span $r</span>) / 2); <span //</span><span 计算中间索引</span>
<span fseek</span>(<span $this</span>->fh, <span $this</span>->first + <span $m</span> * 7<span );
</span><span $beginip</span> = <span strrev</span>(<span fread</span>(<span $this</span>->fh, 4)); <span //</span><span 中间索引的开始IP地址</span>
<span fseek</span>(<span $this</span>->fh, <span $this</span>-><span getLong3());
</span><span $endip</span> = <span strrev</span>(<span fread</span>(<span $this</span>->fh, 4)); <span //</span><span 中间索引的结束IP地址</span>
<span if</span> (<span $ip</span> < <span $beginip</span>) { <span //</span><span 用户的IP小于中间索引的开始IP地址时</span>
<span $r</span> = <span $m</span> - 1<span ;
} </span><span else</span><span  {
</span><span if</span> (<span $ip</span> > <span $endip</span>) { <span //</span><span 用户的IP大于中间索引的结束IP地址时</span>
<span $l</span> = <span $m</span> + 1<span ;
} </span><span else</span> { <span //</span><span 用户IP在中间索引的IP范围内时</span>
<span $findip</span> = <span $this</span>->first + <span $m</span> * 7<span ;
</span><span break</span><span ;
}
}
}
</span><span //</span><span 查询国家地区信息</span>
<span fseek</span>(<span $this</span>->fh, <span $findip</span><span );
</span><span $location</span>['beginip'] = <span long2ip</span>(<span $this</span>->getLong4()); <span //</span><span 用户IP所在范围的开始地址</span>
<span $offset</span> = <span $this</span>-><span getlong3();
</span><span fseek</span>(<span $this</span>->fh, <span $offset</span><span );
</span><span $location</span>['endip'] = <span long2ip</span>(<span $this</span>->getLong4()); <span //</span><span 用户IP所在范围的结束地址</span>
<span $byte</span> = <span fread</span>(<span $this</span>->fh, 1); <span //</span><span 标志字节</span>
<span switch</span> (<span ord</span>(<span $byte</span><span )) {
</span><span case</span> 1: <span //</span><span 国家和区域信息都被重定向</span>
<span $countryOffset</span> = <span $this</span>->getLong3(); <span //</span><span 重定向地址</span>
<span fseek</span>(<span $this</span>->fh, <span $countryOffset</span><span );
</span><span $byte</span> = <span fread</span>(<span $this</span>->fh, 1); <span //</span><span 标志字节</span>
<span switch</span> (<span ord</span>(<span $byte</span><span )) {
</span><span case</span> 2: <span //</span><span 国家信息被二次重定向</span>
<span fseek</span>(<span $this</span>->fh, <span $this</span>-><span getLong3());
</span><span $location</span>['country'] = <span $this</span>-><span getInfo();
</span><span fseek</span>(<span $this</span>->fh, <span $countryOffset</span> + 4<span );
</span><span $location</span>['area'] = <span $this</span>-><span getArea();
</span><span break</span><span ;
</span><span default</span>: <span //</span><span 国家信息没有被二次重定向</span>
<span $location</span>['country'] = <span $this</span>->getInfo(<span $byte</span><span );
</span><span $location</span>['area'] = <span $this</span>-><span getArea();
</span><span break</span><span ;
}
</span><span break</span><span ;
</span><span case</span> 2: <span //</span><span 国家信息被重定向</span>
<span fseek</span>(<span $this</span>->fh, <span $this</span>-><span getLong3());
</span><span $location</span>['country'] = <span $this</span>-><span getInfo();
</span><span fseek</span>(<span $this</span>->fh, <span $offset</span> + 8<span );
</span><span $location</span>['area'] = <span $this</span>-><span getArea();
</span><span break</span><span ;
</span><span default</span>: <span //</span><span 国家信息没有被重定向</span>
<span $location</span>['country'] = <span $this</span>->getInfo(<span $byte</span><span );
</span><span $location</span>['area'] = <span $this</span>-><span getArea();
</span><span break</span><span ;
}
</span><span //</span><span gb2312 to utf-8(去除无信息时显示的CZ88.NET)</span>
<span foreach</span> (<span $location</span> <span as</span> <span $k</span> => <span $v</span><span ) {
</span><span $location</span>[<span $k</span>] = <span str_replace</span>('CZ88.NET','',<span iconv</span>('gb2312', 'utf-8', <span $v</span><span ));
}
</span><span return</span> <span $location</span><span ;
}
</span><span //</span><span 析构函数</span>
<span function</span><span  __destruct() {
</span><span fclose</span>(<span $this</span>-><span fh);
}
}
</span><span $ip</span> = <span new</span><span  ip();
</span><span $addr</span> = <span $ip</span> -> ip2addr('IP地址'<span );
</span><span print_r</span>(<span $addr</span><span );
</span>?>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn