PHP judge IP and jump to the corresponding city sub-station method, IP sub-station
In this paper, we describe the method of PHP to judge IP and jump to the corresponding city sub-station. Share to everyone for your reference. The implementation method is as follows:
<?php class qqwry{var $StartIP = 0; var $EndIP = 0; var $Country = '; var $Local = '; var $CountryFlag =0;//identifies the country location//0x01, followed by a country offset of 3 bytes, no local//0x02, followed by a country offset of 3 bytes, followed by local//other, country,local , Local has 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 ($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, and then comment the following line out of the 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; }}?>
The example shows the following:
$QQWry =new Qqwry; $QQWry->qqwry ("60.31.95.255"); $country = $QQWry->country;echo $QQWry->country. ",". $ qqwry->local;
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/973286.html www.bkjia.com true http://www.bkjia.com/PHPjc/973286.html techarticle PHP judge IP and jump to the corresponding city sub-station method, IP sub-station This example tells the PHP judge IP and jump to the corresponding city sub-station method. Share to everyone for your reference. Concrete implementation of the party ...