PHP convert IP address to real address

Source: Internet
Author: User
Tags chr fread pow query strlen unpack
Born under the red flag long in the spring breeze, long soak in the crab society inside so over time there are some of the way of thinking of the crab, just for a while to do one. NET Web site access statistics system, by the way, how to "monitor" under each of the blogger's location, so there is now the figure shown in accordance with the message received by the IP address of the area where the message is, of course, there is no river Crab Society to monitor the people's meaning, Just to understand the real location of bloggers, in case it is a beautiful blogger can not let me have clues to follow.

Want to convert IPV4 address to real address, must refer to IP database, commercial IP database stored in relational database, query and use is very convenient, but the cost is not individual and small companies willing to bear, So the idea of simple application is to use some free IP database or some large web site to provide the query API, their data is enough for us to use.

1. Using the Pure IP database

Use the local QQWry.Dat file (search to download a QQWry.Dat file into your WordPress theme root), the advantage is that the query speed is very fast, the disadvantage is that the database files should be placed in their own space and occasionally update the database. Time relationship nonsense is not much said, the following is the use of this file function, if it is in WordPress Use this function, the following code to write the subject of the functions.php inside, and then in the comments-list output <?php Echo Convertip (Get_comment_author_ip ());?>. If only the administrator is visible, the calling code <?php if (is_user_logged_in ()) Echo Convertip (Get_comment_author_ip ()),?>. If it is another program reference, enter a valid IPV4 address to get a real address.

The following are the referenced contents:

function Convertip ($IP) {
$dat _path = TemplatePath. " /qqwry.dat ";
if (! $fd = @fopen ($dat _path, "RB")) {
Return ' IP date file not exists or access denied ';
}
$ip = Explode (".", $ip);
$ipNum = $ip [0] * 16777216 + $ip [1] * 65536 + $ip [2] * 256 + $IP [3];
$DataBegin = Fread ($FD, 4);
$DataEnd = Fread ($FD, 4);
$ipbegin = Implode ("", Unpack ("L", $DataBegin));
if ($ipbegin < 0) $ipbegin + = POW (2, 32);
$ipend = Implode ("", Unpack ("L", $DataEnd));
if ($ipend < 0) $ipend + = POW (2, 32);
$ipAllNum = ($ipend-$ipbegin)/7 + 1;
$BeginNum = 0;
$EndNum = $ipAllNum;
while ($ip 1num> $ipNum $ip 2num< $ipNum) {
$Middle = Intval (($EndNum + $BeginNum)/2);
Fseek ($FD, $ipbegin + 7 * $Middle);
$ipData 1 = fread ($FD, 4);
if (strlen ($ipData 1) < 4) {
Fclose ($FD);
Return "System Error";
}
$ip 1num = Implode ("", Unpack ("L", $ipData 1));
if ($ip 1num < 0) $ip 1num + + POW (2, 32);
if ($ip 1num > $ipNum) {
$EndNum = $Middle;
Continue
}
$DataSeek = Fread ($FD, 3);
if (strlen ($DataSeek) < 3) {
Fclose ($FD);
Return "System Error";
}
$DataSeek = Implode ("", Unpack ("L", $DataSeek. Chr (0)));
Fseek ($FD, $DataSeek);
$ipData 2 = fread ($FD, 4);
if (strlen ($ipData 2) < 4) {
Fclose ($FD);
Return "System Error";
}
$ip 2num = Implode ("", Unpack ("L", $ipData 2));
if ($ip 2num < 0) $ip 2num + + POW (2, 32);
if ($ip 2num < $ipNum) {
if ($Middle = = $BeginNum) {
Fclose ($FD);
return "Unknown";
}
$BeginNum = $Middle;
}
}
$ipFlag = Fread ($FD, 1);
if ($ipFlag = = Chr (1)) {
$ipSeek = Fread ($FD, 3);
if (strlen ($ipSeek) < 3) {
Fclose ($FD);
Return "System Error";
}
$ipSeek = Implode ("", Unpack ("L", $ipSeek. Chr (0)));
Fseek ($FD, $ipSeek);
$ipFlag = Fread ($FD, 1);
}
if ($ipFlag = = Chr (2)) {
$AddrSeek = Fread ($FD, 3);
if (strlen ($AddrSeek) < 3) {
Fclose ($FD);
Return "System Error";
}
$ipFlag = Fread ($FD, 1);
if ($ipFlag = = Chr (2)) {
$AddrSeek 2 = fread ($FD, 3);
if (strlen ($AddrSeek 2) < 3) {
Fclose ($FD);
Return "System Error";
}
$AddrSeek 2 = Implode ("", Unpack ("L", $AddrSeek 2.chr (0)));
Fseek ($FD, $AddrSeek 2);
} else {
Fseek ($FD,-1, seek_cur);
}
while (($char = Fread ($FD, 1))!= chr (0))
$ipAddr 2. = $char;
$AddrSeek = Implode ("", Unpack ("L", $AddrSeek. Chr (0)));
Fseek ($FD, $AddrSeek);
while (($char = Fread ($FD, 1))!= chr (0))
$ipAddr 1. = $char;
} else {
Fseek ($FD,-1, seek_cur);
while (($char = Fread ($FD, 1))!= chr (0))
$ipAddr 1. = $char;

$ipFlag = Fread ($FD, 1);
if ($ipFlag = = Chr (2)) {
$AddrSeek 2 = fread ($FD, 3);
if (strlen ($AddrSeek 2) < 3) {
Fclose ($FD);
Return "System Error";
}
$AddrSeek 2 = Implode ("", Unpack ("L", $AddrSeek 2.chr (0)));
Fseek ($FD, $AddrSeek 2);
} else {
Fseek ($FD,-1, seek_cur);
}
while (($char = Fread ($FD, 1))!= chr (0)) {
$ipAddr 2. = $char;
}
}
Fclose ($FD);
if (Preg_match ("/http/i", $ipAddr 2)) {
$ipAddr 2 = "";
}
$IPADDR = "$ipAddr 1 $ipAddr 2";
$IPADDR = Preg_replace ("/cz88.net/is", "", $ipaddr);
$IPADDR = Preg_replace ("/^s*/is", "", $ipaddr);
$IPADDR = Preg_replace ("/s*$/is", "", $ipaddr);
if (Preg_match ("/http/i", $ipaddr) $ipaddr = = "") {
$IPADDR = "Unknown";
}
$IPADDR = Iconv ("GBK", "Utf-8//ignore", $ipaddr);
if ($ipaddr!= "")
return $ipaddr;
Else
$IPADDR = "Address unknown!" Martian visitors? ";
return $ipaddr;
}



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.