Parsing php based on IP query area (very useful, go) _php tutorial

Source: Internet
Author: User
Tags fread unpack
DAT files, information about IP-corresponding regions
Qqwry.dat file
Download yourself Online

class file, parsing the Qqwry.data file
iplocation.php File
Copy CodeThe code is as follows:
Class Iplocation {
/**
* @var Resource Hands
*/
Private $fp;
/**
* The offset address of the first IP record
* @var int
*/
Private $firstip;
/**
* Offset address of last IP record
* @var int
*/
Private $lastip;
/**
* Total number of IP records (not including version information Records)
* @var int
*/
Private $totalip;
/**
* constructor to open the QQWry.Dat file and initialize the information in the class
* @param string $filename
* @return Iplocation
*/
Public function __construct ($filename = "Qqwry.dat") {
$this->FP = 0;
if ($this->FP = @fopen ($filename, ' RB ')!== false) {
$this->firstip = $this->getlong ();
$this->lastip = $this->getlong ();
$this->totalip = ($this->lastip-$this->firstip)/7;
}
}
/**
* Returns the number of read long integers
* @access Private
* @return int
*/
Public Function Getlong () {
Converts the Read Little-endian encoded 4 bytes to a long integer number
$result = Unpack (' Vlong ', fread ($this->FP, 4));
return $result [' Long '];
}
/**
* Returns the number of 3-byte long integers read
*
* @access Private
* @return int
*/
Public Function Getlong3 () {
Converts the Read Little-endian encoded 3 bytes to a long integer number
$result = Unpack (' Vlong ', fread ($this->FP, 3). chr (0));
return $result [' Long '];
}
/**
* Returns the IP address that can be compared after compression
*
* @access Private
* @param string $ip
* @return String
*/
Public Function Packip ($IP) {
Converts the IP address to a long integer and returns false if the IP address is incorrect in PHP5.
At this point intval converts the flase to an integer-1, which is then compressed into Big-endian encoded string
Return pack (' N ', Intval (Ip2long ($IP)));
}
/**
* Returns the Read string
*
* @access Private
* @param string $data
* @return String
*/
Public Function GetString ($data = "") {
$char = Fread ($this->fp, 1);
while (Ord ($char) > 0) {//string is saved in C format and ends in
$data. = $char; Connect prompt the read word after the given string
$char = Fread ($this->fp, 1);
}
Return mb_convert_encoding ($data, ' utf-8 ', ' gb2312 ');
}
/**
* Return Area information
*
* @access Private
* @return String
*/
Public Function Getarea () {
$byte = Fread ($this->fp, 1); Flag byte
Switch (ord ($byte)) {
Case 0://No area information
$area = "";
Break
Case 1:
Case 2://Flag byte 1 or 2 indicating that the zone information is redirected
Fseek ($this->fp, $this->getlong3 ());
$area = $this->getstring ();
Break
Default://Otherwise, indicates that the zone information has not been redirected
$area = $this->getstring ($byte);
Break
}
return $area;
}
/**
* Return the region information according to the IP address or domain name given
* @access Public
* @param string $ip
* @return Array
*/
function GetLocation ($IP) {
if (! $this->FP) return null; If the data file is not opened correctly, return empty
$location [' ip '] = gethostbyname ($IP); Convert the entered domain name to an IP address
$ip = $this->packip ($location [' IP ']); Convert the IP address you entered into a comparable IP address
Illegal IP address will be converted to 255.255.255.255
Search by split
$l = 0; The bottom boundary of the search
$u = $this->totalip; Top boundary of search
$findip = $this->lastip; Returns the last IP record (version information for QQWry.Dat) if it is not found
while ($l <= $u) {//when the upper boundary is less than the lower boundary, the lookup fails
$i = Floor (($l + $u)/2); Calculate approximate intermediate records
Fseek ($this->fp, $this->firstip + $i * 7);
$beginip = Strrev (fread ($this-&GT;FP, 4)); Gets the start IP address of the intermediate record
The purpose of the Strrev function here is to convert the Little-endian's compressed IP address into a Big-endian format.
To be used for comparison, the same later.
if ($ip < $beginip) {//user's IP is smaller than the intermediate record's start IP address
$u = $i-1; Modify the top boundary of the search to the intermediate record minus one
}else{
Fseek ($this->fp, $this->getlong3 ());
$endip = Strrev (fread ($this-&GT;FP, 4)); Gets the end IP address of the intermediate record
if ($ip > $endip) {//user's IP is greater than the end IP address of the intermediate record
$l = $i + 1; Modify the bottom boundary of the search to an intermediate record plus a
}else{//The IP of the user is within the IP range of the intermediate record
$findip = $this->firstip + $i * 7;
Break Indicates that the result is found and exits the loop
}
}
}
Get the IP geolocation information that was found
Fseek ($this->fp, $findip);
$location [' beginip '] = Long2ip ($this->getlong ()); Start address of the range where the user IP is located
$offset = $this->getlong3 ();
Fseek ($this->fp, $offset);
$location [' endip '] = Long2ip ($this->getlong ()); End address of the range where the user IP is located
$byte = Fread ($this->fp, 1); Flag byte
Switch (ord ($byte)) {
Case 1://Flag byte 1, indicating that national and regional information is being redirected at the same time
$countryOffset = $this->getlong3 (); REDIRECT Address
Fseek ($this->fp, $countryOffset);
$byte = Fread ($this->fp, 1); Flag byte
Switch (ord ($byte)) {
Case 2://Flag Byte is 2, indicating that the country information is redirected again
Fseek ($this->fp, $this->getlong3 ());
$location [' country '] = $this->getstring ();
Fseek ($this->fp, $countryOffset + 4);
$location [' area '] = $this->getarea ();
Break
Default://Otherwise, indicates that the country information has not been redirected
$location [' country '] = $this->getstring ($byte);
$location [' area '] = $this->getarea ();
Break
}
Break
Case 2://Flag Byte is 2, indicating that the country information is redirected
Fseek ($this->fp, $this->getlong3 ());
$location [' country '] = $this->getstring ();
Fseek ($this->fp, $offset + 8);
$location [' area '] = $this->getarea ();
Break
Default://Otherwise, indicates that the country information has not been redirected
$location [' country '] = $this->getstring ($byte);
$location [' area '] = $this->getarea ();
Break
}
if ($location [' country '] = = "Cz88.net") {//CZ88. NET means no valid information
$location [' country '] = "Unknown";
}
if ($location [' area '] = = "Cz88.net") {
$location [' area '] = "";
}
return $location;
}

/**
* destructor to automatically close open files after the page execution ends.
*
*/
function __desctruct () {
if ($this->fp) {
Fclose ($this-&GT;FP);
}
$this-&GT;FP = 0;
}
}
?>

This can also be downloaded online, you can copy here, here is also very full.
Execute file, I'm calling ip_location.php file here.
Copy CodeThe code is as follows:
function Getipplace () {
Require_once ("iplocation.php")//Load class file iplocation.php
$ipfile = "Qqwry.dat"; Obtaining information files for IP-corresponding regions
$iplocation = new Iplocation ($ipfile); New Iplocation ($ipfile) $ipfile IP corresponding region information file
$ipresult = $iplocation->getlocation ("IP Address"); Obtaining regional getlocation ("IP Region") based on IP address
return $ipresult;
}
Print_r ($getIpPlace ()); Calling methods
?>

http://www.bkjia.com/PHPjc/327963.html www.bkjia.com true http://www.bkjia.com/PHPjc/327963.html techarticle dat file, about IP corresponding to the region information file Qqwry.dat file on the Internet to download the class file yourself, parse the Qqwry.data file iplocation.php file copy code code is as follows:? PHP cl ...

  • 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.