封装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?>