Class qqwry{ var $StartIP = 0; var $EndIP = 0; var $Country = '; var $Local = '; var $CountryFlag = 0; Identify Country Location 0x01, followed by 3 bytes of country offset, no local 0x02, followed by a country offset of 3 bytes, followed by the local Other, country,local,local have similar compression. Possible multiple references. var $fp; var $FirstStartIp = 0; var $LastStartIp = 0; var $EndIpOff = 0; function Getstartip ($RecNo) { $offset = $this->firststartip+ $RecNo * 7; @fseek ($this->fp, $offset, Seek_set); $buf =fread ($this->FP, 7); $this->endipoff=ord ($buf [4]) + (ORD ($buf [5]) *256) + (ord ($buf [6]) *256*256); $this->startip=ord ($buf [0]) + (ORD ($buf [1]) *256) + (Ord ($buf [2]) *256*256) + (Ord ($buf [3]) *256*256*256); return $this->startip; } function Getendip () { @fseek ($this->fp, $this->endipoff, Seek_set); $buf =fread ($this->FP, 5); $this->endip=ord ($buf [0]) + (ORD ($buf [1]) *256) + (Ord ($buf [2]) *256*256) + (Ord ($buf [3]) *256*256*256); $this->countryflag=ord ($buf [4]); return $this->endip; } function Getcountry () { Switch ($this->countryflag) { Case 1: Case 2: $this->country= $this->getflagstr ($this->endipoff+4); echo sprintf (' endipoffset= (%x) ', $this->endipoff); $this->local= (1 = = $this->countryflag)? ": $this->getflagstr ($this->endipoff+8); break; Default: $this->country= $this->getflagstr ($this->endipoff+4); $this->local= $this->getflagstr (Ftell ($this->fp)); } } function Getflagstr ($offset) { $flag = 0; while (1) { @fseek ($this->fp, $offset, Seek_set); $flag =ord (fgetc ($this->fp)); if ($flag = = 1 | | $flag = = 2) { $buf =fread ($this->FP, 3); if ($flag ==2) { $this->countryflag=2; $this->endipoff= $offset-4; } $offset =ord ($buf [0]) + (ORD ($buf [1]) *256) + (Ord ($buf [2]) * 256*256); } else{ break; } } if ($offset <12) Return '; @fseek ($this->fp, $offset, Seek_set); return $this->getstr (); } function Getstr () { $str = "; while (1) { $c =fgetc ($this->FP); echo "$CN"; if (Ord ($c [0]) = = 0) break; $str. = $c; } echo "$str n"; return $str; } function Qqwry ($dotip = ") { if (!is_string ($dotip) | | $dotip = = ") {return;} if (Preg_match ("/^127/", $dotip)) {$this->country= "local network"; return;} ElseIf (Preg_match ("/^192/", $dotip)) {$this->country= "LAN"; return;} $nRet; $ip = $this->iptoint ($dotip); $this->fp= fopen (__qqwry__, "RB"); if ($this->FP = = NULL) { $szLocal = "Openfileerror"; return 1; } @fseek ($this->fp, 0, Seek_set); $buf =fread ($this->FP, 8); $this->firststartip=ord ($buf [0]) + (ORD ($buf [1]) *256) + (Ord ($buf [2]) *256*256) + (Ord ($buf [3]) *256*256*256); $this->laststartip=ord ($buf [4]) + (ORD ($buf [5]) *256) + (ord ($buf [6]) *256*256) + (ord ($buf [7]) *256*256*256); $RecordCount = Floor (($this->laststartip-$this->firststartip)/7); if ($RecordCount <= 1) { $this->country= "Filedataerror"; Fclose ($this->FP); return 2; } $RangB = 0; $RangE = $RecordCount; Match ... while ($RangB < $RangE-1) { $RecNo = Floor (($RangB + $RangE)/2); $this->getstartip ($RecNo); if ($ip = = $this->startip) { $RangB = $RecNo; break; } if ($ip > $this->startip) $RangB = $RecNo; Else $RangE = $RecNo; } $this->getstartip ($RangB); $this->getendip (); if (($this->startip <= $ip) && ($this->endip >= $ip)) { $nRet = 0; $this->getcountry (); It's not so good ....... So.......... $this->local=str_replace ("area error", "", $this->local); } else{ $nRet = 3; $this->country= ' unknown '; $this->local= '; } Fclose ($this->FP); $this->country=preg_replace ("/(cz88.net) | ( Pure network)/"," LAN/Unknown ", $this->country); $this->local=preg_replace ("/(cz88.net) | ( Pure network)/"," LAN/Unknown ", $this->local); Take a look at what the value of the $nRet above is 0 and 3, then comment out the following line return $nRet; Return "$this->country $this->local"; #如此直接返回位置和国家便可以了 } function Iptoint ($Ip) { $array =explode ('. ', $Ip); $Int = ($array [0] * 256*256*256) + ($array [1]*256*256) + ($array [2]*256) + $array [3]; return $Int; } } ?> |