<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>ip Place of Ownership </title>
<body>
<form id= "Form1" Name= "Form1" method= "Post" action= "" >
<input type= "text" name= "Ipinfo" id= "Ipinfo" style= "border:solid 1px #fdbdd5"/>
<input type= "Submit" name= "button" id= button "value=" Query "style=" Border:solid 1px #fdbdd5; width:50px "/>"
</form>
<?php Tutorial
Include ("ipcheck.class.php");
$ipinfo =$_post[' Ipinfo '];
$iptest =new iplocation ();
echo $iptest->getlocation ($ipinfo);
?>
</body>
The ipcheck.class.php code is as follows
<?php
Class Iplocation {
/**
* QQWry.Dat file pointer
* @var Resource
*/
var $fp;
/**
* Offset address of the first IP record
*
* @var int
*/
var $firstip;
/**
* Offset address of the last IP record
*
* @var int
*/
var $lastip;
/**
* Total number of IP records (does not contain version information records)
*
* @var int
*/
var $totalip;
/**
* Returns the long integer read
*
* @access Private
* @return int
*/
function Getlong () {
Converts 4 bytes of read Little-endian encoded into long integer numbers
$result = Unpack (' Vlong ', fread ($this->FP, 4));
return $result [' Long '];
}
/**
* Returns the 3-byte long integer read
*
* @access Private
* @return int
*/
function Getlong3 () {
Converts 3 bytes of read Little-endian encoded into long integer numbers
$result = Unpack (' Vlong ', fread ($this->FP, 3). chr (0));
return $result [' Long '];
}
/**
* Returns the compressed IP address that can be compared after compression
*
* @access Private
* @param string $ip
* @return String
*/
function Packip ($IP) {
Converts an IP address to a long integer 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
*/
function GetString ($data = "") {
$char = Fread ($this->fp, 1);
while (Ord ($char) > 0) {//string is saved in C format to end
$data. = $char; After fonts the read Word to the given string
$char = Fread ($this->fp, 1);
}
return $data;
}
/**
* Return to regional information
*
* @access Private
* @return String
*/
function Getarea () {
$byte = Fread ($this->fp, 1); Flag byte
Switch (ord ($byte)) {
Case 0://No regional 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 zone information is not redirected
$area = $this->getstring ($byte);
Break
}
return $area;
}
/**
* Return the area information according to the given IP address or domain name
*
* @access Public
* @param string $ip
* @return Array
*/
function GetLocation ($IP) {
if (! $this->FP) return null; If the data file is not opened correctly, return null directly
$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
An illegal IP address is converted to 255.255.255.255.
Pair-Search
$l = 0; Search the bottom boundary
$u = $this->totalip; Top bounds for Search
$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 records
Fseek ($this->fp, $this->firstip + $i * 7);
$beginip = Strrev (fread ($this->FP, 4)); Get start IP address of intermediate record
The Strrev function here is to convert Little-endian's compressed IP address to Big-endian format
For comparison purposes, 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) {//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 {//user's IP is within the IP range of the intermediate record
$findip = $this->firstip + $i * 7;
Break Indicates that the result is found and the loop is exited
}
}
}
Get the IP geographic information found
Fseek ($this->fp, $findip);
$location [' beginip '] = Long2ip ($this->getlong ()); Start address of the range where the user IP resides
$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 is redirected simultaneously
$countryOffset = $this->getlong3 (); REDIRECT Address
Fseek ($this->fp, $countryOffset);
$byte = Fread ($this->fp, 1); Flag byte
Switch (ord ($byte)) {
Case 2://Sign Byte 2, indicating that country information is redirected
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 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 the country information has not been 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;
echo "Your IP:". $location [' IP ']. " <br/> from ". $location [' Country ']. $location [' area '];
}
/**
* constructor, open the QQWry.Dat file and initialize the information in the class
*
* @param string $filename
* @return Iplocation
*/
function iplocation ($filename = "QQWry.Dat") {
if ($this->FP = @fopen ($filename, ' RB ')!== false) {
$this->firstip = $this->getlong ();
$this->lastip = $this->getlong ();
$this->totalip = ($this->lastip-$this->firstip)/7;
Registers a destructor so that it executes at the end of the program execution
Register_shutdown_function (Array (& $this, ' _iplocation '));
}
}
/**
* destructor, used to automatically close open files after the end of a page execution.
*
*/
function _iplocation () {
Fclose ($this->FP);
}
}
?>