The
looks like this:
<?php $data = ' 254.254.254.254 ';
echo Ip2long ($data); function GetIP () {if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "unknown") $ip = ge
Tenv ("Http_client_ip"); ElseIf (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_for"), "unknown") $ip = getenv
("Http_x_forwarded_for");
ElseIf (getenv ("REMOTE_ADDR") && strcasecmp (getenv ("REMOTE_ADDR"), "unknown") $ip = getenv ("REMOTE_ADDR"); ElseIf (Isset ($_server[' remote_addr ']) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_
ADDR '], "unknown") $ip = $_server[' remote_addr '];
else $ip = "0.0.0.0";
return $IP;
Class Iplocation {//data file pointer var $fp;
var $firstip;
var $lastip;
var $totalip; function Getlong () {//unpack unpack data from binary strings//converts the Read Little-endian encoded 4 bytes to a long integer, fread reads the binary file safely $result = unpack (
' Vlong ', fread ($this->FP, 4));
return $result [' Long ']; } FuNction Getlong3 () {///converts 3 bytes of the read Little-endian encoding to a long integer $result = unpack (' Vlong ', fread ($this->FP, 3). chr (0));
return $result [' Long ']; The function Packip ($IP) {//pack loads data into a binary string//ip2long the IP address to an unsigned long integer, or it can be used to authenticate the IP address Return pack (' N ', Intva
L (Ip2long ($IP)));
function GetString ($data = "") {$char = Fread ($this->fp, 1);
while (Ord ($char) > 0) {//ord Returns the ASCII value of the character, the string is saved in C format, and the $data is ended with a. = $char;
$char = Fread ($this->fp, 1);
return $data; 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 area 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;
} function GetLocation ($IP) {if (! $this->FP) return null; If the data file is not opened correctly, the null $location [' IP '] = gethostbyname ($IP) is returned directly; Domain name translates to IP address $ip = $this->packip ($location [' IP ']); Converting the entered IP address to a comparable IP address//illegal IP address is converted to 255//pair-Search $l = 0; $u of the bottom boundary of the search = $this->totalip; Search on the upper boundary $findip = $this->lastip; Returns the last IP record (QQWry.Dat version information if not found) while ($l <= $u) {//when the upper boundary is less than the bottom boundary, the lookup fails $i = Floor (($l + $u)/2);
/compute 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 inversion string//Strrev function here is to convert Little-endian compressed IP address to Big-endian format for easy comparison//about Little-endian and BI G-endian Reference: http://baike.baidu.com/view/2368412.htm 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)); 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 the middle record plus one} else {//user's IP in the intermediate recorded IP range $findip = $this->firstip + $i * 7; Break
To find the result, exit the Loop}}} fseek ($this->fp, $findip); $location [' beginip '] = Long2ip ($this->getlong ());
The starting address of the User IP range $offset = $this->getlong3 ();
Fseek ($this->fp, $offset); $location [' endip '] = Long2ip ($this->getlong ()); The end address of the user IP range $byte = fread ($this->fp, 1); The flag byte switch (ORD ($byte)) {Case 1://Flag Byte is 1, indicating that both country and zone information are redirected at the same time $countryOffset = $this->ge Tlong3 ();
REDIRECT Address fseek ($this->fp, $countryOffset); $byte = Fread ($this->fp, 1); The flag byte switch (ORD ($byte)) {Case 2://Flag Byte is 2, indicating that national information is redirected fseek ($this->fp, $this-&
Gt;getlong3 ()); $location [' country '] = $this->getString ();
Fseek ($this->fp, $countryOffset + 4);
$location [' area '] = $this->getarea ();
Break
Default://Otherwise, the state information is not redirected $location [' country '] = $this->getstring ($byte);
$location [' area '] = $this->getarea ();
Break
} break;
Case 2://Sign 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, the state information is not redirected $location [' country '] = $this->getstring ($byte);
$location [' area '] = $this->getarea ();
Break
} if ($location [' country '] = = "Cznet") {//cznet indicates no valid information $location [' country '] = "Unknown";
if ($location [' area '] = = "Cznet") {$location [' area '] = "";
return $location; /** * constructor, open QQWry.Dat file and startThe information in the initialization class */function iplocation ($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;
Registers the destructor so that it executes Register_shutdown_function (Array (& $this, ' _iplocation ') at the end of the execution of the program; }/** * destructor, used to automatically close open files after the end of page execution/function _iplocation () {if ($this->fp) {fclose ($this-&
GT;FP);
} $this->fp = 0; } header ("content-type:text/html;
CHARSET=GBK ");
$ipOrDomain = ' 110.0.0.0 ';
$ipOrDomain = ' www.baidu.com ';
$iplocation = new Iplocation ();
$location = $iplocation->getlocation ($ipOrDomain);
$address =mb_convert_encoding ($location [' Country ']. $location [' area '], "GBK", "GBK");
Echo $address; ?>
Above this PHP to obtain the location of IP address information (using the pure IP database qqwry.dat) is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.