PHP利用纯真IP数据库在本地实现IP地址信息查询

封装IP函数

1<?php 2class cls_ipAddress{ 3 private $fp; 4 private $firstip; 5 private $lastip; 6 private $totalip; 7 8 public function __construct($filename="qqwry.dat"){ 9 $this->fp=0; 10 if(($this->fp=@fopen($filename,"rb"))!==false){ 11 $this->firstip=$this->getlong(); 12 $this->lastip=$this->getlong(); 13 $this->totalip=($this->lastip-$this->firstip)/7; 14 register_shutdown_function(array(&$this,"__destruct")); 15 } 16 } 17 18 public function __destruct(){ 19 if($this->fp){ 20 @fclose($this->fp); 21 } 22 $this->fp=0; 23 } 24 25 private function getlong(){ 26 $result=unpack("Vlong",fread($this->fp,4)); 27 return $result["long"]; 28 } 29 30 private function getlong3(){ 31 $result=unpack("Vlong",fread($this->fp,3).chr(0)); 32 return $result["long"]; 33 } 34 35 private function packip($ip){ 36 return pack("N",intval(ip2long($ip))); 37 } 38 39 private function getstring($data=""){ 40 $char=fread($this->fp,1); 41 while(ord($char)>0){ 42 $data.=$char; 43 $char=fread($this->fp,1); 44 } 45 return $data; 46 } 47 48 private function getarea(){ 49 $byte=fread($this->fp,1); 50 switch(ord($byte)){ 51 case 0: 52 $operators=""; 53 break; 54 case 1: 55 case 2: 56 fseek($this->fp,$this->getlong3()); 57 $operators=$this->getstring(); 58 break; 59 default: 60 $operators=$this->getstring($byte); 61 break;} 62 return $operators; 63 } 64 65 public function getlocation($ip){ 66 if(!$this->fp){return null;} 67 $location["ip"]=gethostbyname($ip); 68 $ip=$this->packip($location["ip"]); 69 $l=0; 70 $u=$this->totalip; 71 $findip=$this->lastip; 72 while($l<=$u){ 73 $i=floor(($l+$u)/2); 74 fseek($this->fp,$this->firstip+$i*7); 75 $startip=strrev(fread($this->fp,4)); 76 if($ip<$startip){ 77 $u=$i-1; 78 }else{ 79 fseek($this->fp,$this->getlong3()); 80 $endip=strrev(fread($this->fp,4)); 81 if($ip>$endip){ 82 $l=$i+1; 83 }else{ 84 $findip=$this->firstip+$i*7; 85 break; 86 } 87 } 88 } 89 fseek($this->fp,$findip); 90 $location["startip"]=long2ip($this->getlong()); 91 $offset=$this->getlong3(); 92 fseek($this->fp,$offset); 93 $location["endip"]=long2ip($this->getlong()); 94 $byte=fread($this->fp,1); 95 switch(ord($byte)){ 96 case 1: 97 $countryOffset=$this->getlong3(); 98 fseek($this->fp,$countryOffset); 99 $byte=fread($this->fp,1); 100 switch(ord($byte)){ 101 case 2: 102 fseek($this->fp,$this->getlong3()); 103 $location["area"]=$this->getstring(); 104 fseek($this->fp,$countryOffset+4); 105 $location["operators"]=$this->getarea(); 106 break; 107 default: 108 $location["area"]=$this->getstring($byte); 109 $location["operators"]=$this->getarea(); 110 break;} 111 break; 112 case 2: 113 fseek($this->fp,$this->getlong3()); 114 $location["area"]=$this->getstring(); 115 fseek($this->fp,$offset+8); 116 $location["operators"]=$this->getarea(); 117 break; 118 default: 119 $location["area"]=$this->getstring($byte); 120 $location["operators"]=$this->getarea(); 121 break;} 122 if($location["area"]=="CZ88.NET"){ 123 $location["area"]="未知"; 124 } 125 if($location["operators"]=="CZ88.NET"){ 126 $location["operators"]="未知"; 127 } 128 return $location; 129 } 130} 131?>

调用IP函数

1<?php 2$ip="220.186.136.207"; 3$setip=new cls_ipAddress("qqwry.dat"); 4$location=$setip->getlocation($ip); 5print_r($location); 6?>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid

PHP利用纯真IP数据库在本地实现IP地址信息查询 - HelloWorld