DAT files, information files about IP-corresponding areas
Qqwry.dat file
Download yourself Online
class file, parsing the Qqwry.data file's
iplocation.php File
Copy Code code as follows:
<?php
class Iplocation {
/**
* @var Resource pointer
*/
private $fp;
/**
* The offset address of the first IP record
* @var int
*/
private $firstip;
/**
* Offset address of the last IP record
* @var int
*/
private $lastip;
/**
Total number of
* IP records (does not contain version information records)
* @var int
*/
private $totalip;
/**
the * constructor, opens the QQWry.Dat file and initializes 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 long integer number of reads
* @access Private
* @return int
*/
Public Function Getlong () {
//Converts the Read Little-endian encoded 4 bytes to a long integer
$result = Unpack (' Vlong ', fread ($this->FP, 4));
return $result [' Long '];
}
/**
* Returns the 3-byte long integer read
*
* @access Private
* @return int
*/
Public Function Getlong3 () {
//Converts the Read Little-endian encoded 3 bytes to a long integer
$result = Unpack (' Vlong ', fread ($this->FP, 3). chr (0));
return $result [' Long '];
}
/**
* Returns an 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 number, and returns False if the IP address is incorrect in PHP5,
//Then Intval converts flase to an integer-1, which is then compressed into a 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 to end
$data. = $char; Fonts the read character to the given string
$char = fread ($this->fp, 1);
}
Return mb_convert_encoding ($data, ' utf-8 ', ' gb2312 ');
}
/**
* Return to area information
*
* @access Private
* @return String
*/
Public Function Getarea () {
$byte = fread ($this->fp, 1); Flag byte
Switch (ord ($byte)) {
case 0://no zone information
$area = "";
break;
Case 1:
Case 2://The flag byte is 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 is not redirected
$area = $this->getstring ($byte);
break;
}
return $area;
}
/**
* Returns the region information based on the given IP address or domain name
* @access Public
* @param string $ip
* @return Array
*/
function GetLocation ($ip) {
if (! $this->FP) return null; Returns an empty
if the data file is not opened correctly
$location [' ip '] = gethostbyname ($IP); Converts the domain name entered into an IP address
$ip = $this->packip ($location [' IP ']); Converts the IP address entered into a comparable IP address
//Illegal IP address will be converted to 255.255.255.255
//Split search
$l = 0; Search the bottom boundary
$u = $this->totalip; Search on the top boundary
$findip = $this->lastip; Returns the last IP record (QQWry.Dat version information) if it is not found
while ($l <= $u) {//When the top boundary is less than the bottom boundary, the lookup fails
$i = Floor (($l + $u)/2); Calculating approximate intermediate record
fseek ($this->fp, $this->firstip + $i * 7);
$beginip = Strrev (fread ($this->FP, 4)); Gets the start IP address of the intermediate record
The
//Strrev function here is to convert Little-endian compressed IP address to Big-endian format
//For comparison, followed by the same.
if ($ip < $beginip) {//The user's IP is less than the start IP address of the intermediate record
$u = $i-1; Modifies the top boundary of the search to intermediate records minus one
}else{
fseek ($this->fp, $this->getlong3 ());
$endip = Strrev (fread ($this->FP, 4)); Get the end IP address of an intermediate record
if ($ip > $endip) {//The IP of the user 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{//user's IP is within the IP range of the intermediate record
$findip = $this->firstip + $i * 7;
break; To find the result, exit the loop
}
}
}
//Get the IP geographic information found
fseek ($this->fp, $findip);
$location [' beginip '] = Long2ip ($this->getlong ()); Start address of User IP range
$offset = $this->getlong3 ();
fseek ($this->fp, $offset);
$location [' endip '] = Long2ip ($this->getlong ()); End address of User IP range
$byte = fread ($this->fp, 1); Flag byte
Switch (ord ($byte)) {
Case 1://The flag byte is 1, indicating that both national and regional information are redirected simultaneously
$countryOffset = $this->getlong3 (); REDIRECT Address
Fseek ($this->fp, $countryOffset);
$byte = fread ($this->fp, 1); Flag byte
Switch (ord ($byte)) {
Case 2://The flag byte is 2, indicating that the country information has been redirected
fseek ($this->fp, $this->getlong3 ());
$location [' country '] = $this->getstring ();
fseek ($this->fp, $countryOffset + 4);
$location [' area '] = $this->getarea ();
break;
Default://Otherwise, indicates that national information is not redirected
$location [' country '] = $this->getstring ($byte);
$location [' area '] = $this->getarea ();
break;
}
break;
Case 2://The flag byte is 2, indicating that the national 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 national information is not redirected
$location [' country '] = $this->getstring ($byte);
$location [' area '] = $this->getarea ();
break;
}
if ($location [' country '] = = "Cz88.net") {//CZ88. NET indicates no valid information
$location [' country '] = "Unknown";
}
if ($location [' area '] = = "Cz88.net") {
$location [' area '] = ' ";
}
return $location;
}
/**
* destructor, used to automatically close open files after the end of the page execution.
*
*/
function __desctruct () {
if ($this->fp) {
fclose ($this->FP);
}
$this->fp = 0;
}
}
?>
This also can download online, also can copy here, here is also very full.
Execute file, I'm here called ip_location.php file
Copy Code code as follows:
<?php
Function Getipplace () {
require_once ("iplocation.php")/Load class file iplocation.php
$ipfile = " Qqwry.dat "; Gets the information file for the IP-corresponding region
$iplocation = new Iplocation ($ipfile);//new iplocation ($ipfile) $ipfile IP corresponding region information file
$ipresult = $ Iplocation->getlocation ("IP Address"); Obtain the locale getlocation ("IP region")
return $ipresult;
based on the IP address
Print_r ($getIpPlace ());//Calling method
?