PHP利用纯真IP数据库在本地实现IP地址信息查询
2013-07-22 00:00
323 查看
封装IP函数
调用IP函数
<?php class cls_ipAddress{ private $fp; private $firstip; private $lastip; private $totalip; public function __construct($filename="qqwry.dat"){ $this->fp=0; if(($this->fp=@fopen($filename,"rb"))!==false){ $this->firstip=$this->getlong(); $this->lastip=$this->getlong(); $this->totalip=($this->lastip-$this->firstip)/7; register_shutdown_function(array(&$this,"__destruct")); } } public function __destruct(){ if($this->fp){ @fclose($this->fp); } $this->fp=0; } private function getlong(){ $result=unpack("Vlong",fread($this->fp,4)); return $result["long"]; } private function getlong3(){ $result=unpack("Vlong",fread($this->fp,3).chr(0)); return $result["long"]; } private function packip($ip){ return pack("N",intval(ip2long($ip))); } private function getstring($data=""){ $char=fread($this->fp,1); while(ord($char)>0){ $data.=$char; $char=fread($this->fp,1); } return $data; } private function getarea(){ $byte=fread($this->fp,1); switch(ord($byte)){ case 0: $operators=""; break; case 1: case 2: fseek($this->fp,$this->getlong3()); $operators=$this->getstring(); break; default: $operators=$this->getstring($byte); break;} return $operators; } public function getlocation($ip){ if(!$this->fp){return null;} $location["ip"]=gethostbyname($ip); $ip=$this->packip($location["ip"]); $l=0; $u=$this->totalip; $findip=$this->lastip; while($l<=$u){ $i=floor(($l+$u)/2); fseek($this->fp,$this->firstip+$i*7); $startip=strrev(fread($this->fp,4)); if($ip<$startip){ $u=$i-1; }else{ fseek($this->fp,$this->getlong3()); $endip=strrev(fread($this->fp,4)); if($ip>$endip){ $l=$i+1; }else{ $findip=$this->firstip+$i*7; break; } } } fseek($this->fp,$findip); $location["startip"]=long2ip($this->getlong()); $offset=$this->getlong3(); fseek($this->fp,$offset); $location["endip"]=long2ip($this->getlong()); $byte=fread($this->fp,1); switch(ord($byte)){ case 1: $countryOffset=$this->getlong3(); fseek($this->fp,$countryOffset); $byte=fread($this->fp,1); switch(ord($byte)){ case 2: fseek($this->fp,$this->getlong3()); $location["area"]=$this->getstring(); fseek($this->fp,$countryOffset+4); $location["operators"]=$this->getarea(); break; default: $location["area"]=$this->getstring($byte); $location["operators"]=$this->getarea(); break;} break; case 2: fseek($this->fp,$this->getlong3()); $location["area"]=$this->getstring(); fseek($this->fp,$offset+8); $location["operators"]=$this->getarea(); break; default: $location["area"]=$this->getstring($byte); $location["operators"]=$this->getarea(); break;} if($location["area"]=="CZ88.NET"){ $location["area"]="未知"; } if($location["operators"]=="CZ88.NET"){ $location["operators"]="未知"; } return $location; } } ?>
调用IP函数
<?php $ip="220.186.136.207"; $setip=new cls_ipAddress("qqwry.dat"); $location=$setip->getlocation($ip); print_r($location); ?>
相关文章推荐
- PHP利用纯真IP数据库在本地实现IP地址信息查询
- PHP利用纯真IP数据库在本地实现IP地址信息查询
- [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(四. iOS端代码实现)
- iOS实战演练之关于数据交互的问题(利用XAMPP搭建本地服务器, 编写PHP代码实现iOS端注册登录功能)
- 纯真ip数据库查询的php实现(补充分组查询)
- [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现)
- 利用PHP实现手机号码归属地查询程序
- php实现利用phpexcel导出数据
- PHP巧妙利用位运算实现网站权限管理
- 利用php拼接图片,实现水印的效果
- 利用php cookie实现浏览历史功能
- 利用 gettext 来实现 PHP 的国际化编程
- PHP实现远程下载文件到本地
- php 下载保存文件保存到本地的两种实现方法
- 利用ajax实现与php(json数组)数据交互,并局部刷新页面
- 利用PHP SOAP实现WEB SERVICE
- PHP利用新浪IP接口,来实现IP地址定位具体城市
- 利用PHP结合GD实现图片裁剪和缩略图功能
- PHP利用CURL_MULTI实现多线程爆破
- php读取纯真ip数据库使用示例