PHP read qqwry.dat IP address location File class instance code _php instance

Source: Internet
Author: User

The

instance is as follows:

<?php//+----------------------------------------------------------------------//|
// +----------------------------------------------------------------------
// | +----------------------------------------------------------------------class iplocate{var $fp; var $firstip;// The offset address of the first IP index, var $lastip; The offset address of the last IP index, var $totalip; Total IP number//Get client IP address function get_client_ip () {if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_
IP ")," unknown ") $ip = getenv (" Http_client_ip "); else if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "unknown") $ip = getenv
("Http_x_forwarded_for");
else if (getenv ("REMOTE_ADDR") && strcasecmp (getenv ("REMOTE_ADDR"), "unknown") $ip = getenv ("REMOTE_ADDR"); else if (isset ($_server[' remote_addr ']) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_
ADDR '], "unknown") $ip = $_server[' remote_addr '];
else $ip = "Unknown";
return ($IP); //*//constructor, initialize some variables//$DATFThe value of Ile is the name of the pure IP database, which can be modified by itself. * Function Iplocate () {$datfile = "ip1.dat"; $this->fp=fopen ($datfile, ' RB ');//Two mode open $this->firstip = $this- &GT;GET4B (); The absolute offset address of the first IP index $this->lastip = $this->get4b (); The absolute offset address of the last IP index $this->totalip = ($this->lastip-$this->firstip)/7; The IP total index area is a fixed length of 7 bytes, which is divided by 7, register_shutdown_function (Array ($this, "CLOSEFP"));
In order to be compatible with the following versions, this class does not automatically close the IP library with destructors PHP5. //*///Turn off IP library//* function closefp () {fclose ($this->fp);//*//Read 4 bytes and will extract the long mode of the @unpack to Long () {$str)
("V", Fread ($this->fp,4));
return $STR [1]; //*//Read redirected offset address//* function GetOffset () {$str = @unpack ("V", Fread ($this->fp,3). chr (0)); return $str [1];}//*/Read I The detailed address information for P//* function getstr () {$split =fread ($this->fp,1); while (Ord ($split)!=0) {$str. = $split; $split =fread ($
this->fp,1);
return $str; //*///Transfer IP via Ip2long to IPv4 's Internet address, and then compress it into Big-endian byte sequence//used to compare the IP address in the index area//* function Iptoint ($IP) {return Pack ("N"),
Intval (Ip2long ($IP))); //*//Get client IP address//NOTE: ifYou want to log the IP to the server, please check the IP data security when you write the library. 
* Function GetIP () {if (getenv (' http_client_ip ')) {$ip = getenv (' http_client_ip '); } elseif (getenv (' http_x_forwarded_for ')) {//Get the real IP address of the client with a proxy server $ip = getenv (' http_x_forwarded_for '); ElseIf (Gete NV (' http_x_forwarded ')) {$ip = getenv (' http_x_forwarded ');} elseif (getenv (' http_forwarded_for ')) {$ip = getenv (' Http_ 
Forwarded_for '); } elseif (getenv (' http_forwarded ')) {$ip = getenv (' http_forwarded ');} else {$ip = $_server[' remote_addr ']; return $i
P
//*//Get address information//* function readaddress () {$now _offset=ftell ($this-&GT;FP);//get current pointer address $flag = $this->getflag (); Switch (ord ($flag)) {case 0: $address = ""; 1:case 2:fseek ($this->fp, $this->getoffset ()); $address = $thi
S->getstr ();
Break
Default:fseek ($this->fp, $now _offset);
$address = $this->getstr ();
Break
return $address;
//*//Get flag 1 or 2//To determine whether the address is redirected. * Function Getflag () {return fread ($this->fp,1); the//*///Binary lookup method searches the index area for IP//* function searchip ($ip{$ip =gethostbyname ($IP);//change domain name to IP $ip _offset["IP"]= $ip; $ip = $this->iptoint ($IP);//convert IP to grow int $firstip = 0;// Search on the upper boundary $lastip = $this->totalip; $ipoffset of the bottom boundary of the search = $this->lastip; The offset address that is initialized to the last IP address while ($firstip <= $lastip) {$i =floor (($firstip + $lastip)/2)//COMPUTE an approximate intermediate record floor function to calculate the maximum number of decimal numbers for a given floating-point number, say White is the four homes and five fseek ($this->fp, $this->firstip + $i * 7); Position pointer to intermediate record $startip =strrev (fread ($this->fp,4)); Reads the start IP address in the current index area and converts its Little-endian byte order to Big-endian if ($ip < $startip) {$lastip = $i-1;} else {fseek ($this->
FP, $this->getoffset ());
$endip =strrev (fread ($this->fp,4));
if ($ip > $endip) {$firstip = $i + 1;} else {$ip _offset["offset"]= $this->firstip + $i * 7; break;}}
return $IP _offset; //*//Get IP Address details//* function getaddress ($ip) {$ip _offset= $this->searchip ($IP);//Get the absolute IP address in the index area $ipoffset = $ip _
offset["offset"];
$address ["IP"]= $ip _offset["IP"]; Fseek ($this->fp, $ipoffset); Navigate to the index area $address ["StartIP"]=long2ip ($this->get4b ()); Start IP in index area Address $address _offset= $this->getoffset (); Gets the offset address fseek ($this->fp, $address _offset) of IP in the IP record area in the index area; Navigate to the record area $address ["EndIP"]=long2ip ($this->get4b ()); The end IP address in the record area $flag = $this->getflag (); Read Flag byte switch (ORD ($flag)) {Case 1://Region 1 area 2 are redirected $address _offset= $this->getoffset ()/Read redirect address fseek ($this-&GT;FP , $address _offset); Locate the pointer to the redirected address $flag = $this->getflag (); Read Flag byte switch (ORD ($flag)) {Case 2://Region 1 redirect again, fseek ($this->fp, $this->getoffset ()); $address [area1]]=$
This->getstr (); Fseek ($this->fp, $address _offset+4); Jump 4 bytes $address ["area2"]= $this->readaddress ();
Area 2 may be redirected, there may not be a break; Default://area 1, Area 2 no redirect fseek ($this->fp, $address _offset);
Locate the pointer to the redirected address $address ["area1"]= $this->getstr ();
$address ["Area2"]= $this->readaddress ();
Break
} break; Case 2://area 1 Redirect Area 2 No redirect $address 1_offset= $this->getoffset (); 
Read REDIRECT Address fseek ($this->fp, $address 1_offset);
$address ["area1"]= $this->getstr ();
Fseek ($this->fp, $address _offset+8); $address["Area2"]= $this->readaddress ();
Break
Default://area 1 Area 2 no redirect fseek ($this->fp, $address _offset+4);
$address ["area1"]= $this->getstr ();
$address ["Area2"]= $this->readaddress ();
Break //* filter Some unwanted data if (Strpos ($address ["area1"], "cz88.net")!=false) {$address ["area1"]= "Unknown";} if (Strpos ($address ["AREA2"
], "cz88.net")!=false {$address ["area2"]= "";} 
foreach ($address as $k => $item) {if (! $this->is_utf8 ($address [$k])) {$address [$k] = iconv (' GBK ', ' utf-8 ', $item);}
return $address; function Is_utf8 ($string) {return Preg_match ('%^ (?: [\x09\x0a\x0d\x20-\x7e] # ASCII | [\XC2-\XDF] [\X80-\XBF] # Non-overlong 2-byte | \XE0[\XA0-\XBF][\X80-\XBF] # excluding Overlongs | [\xe1-\xec\xee\xef] [\X80-\XBF] {2} # straight 3-byte | \XED[\X80-\X9F][\X80-\XBF] # excluding Surrogates | \XF0[\X90-\XBF][\X80-\XBF]{2} # Planes 1-3 | [\xf1-\xf3] [\X80-\XBF] {3} # planes 4-15 |
\XF4[\X80-\X8F][\X80-\XBF]{2} # Plane) *$%xs ', $string); }}?>

Above this PHP read qqwry.dat IP address location File class instance code is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.