FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


An ip 2 location system in design, interesting find





Mgccl
There is a lot ip2location systems out there, there is one Chinese database which is so powerful it maped out almost everything in CHINA... and there are some more
From
http://hknng.3322.org/hknng/archive.php/article/286.html
It's Chinese so you won't understand. I just want to expand on that a little by constracting some more IP2location databases
If, search the Chinese database and get no result, it will use the one down here
http://www.hostip.info/dl/index.html
and Then, the ip 2 country database(could be the best out there)
http://Software77.net

If anyone need the QQwry.dat, you can ask me for it.
strangely, QQwry.dat uses some compression, that makes it ULTRA fast to load. with the function down here, which uses a flat file data, it can get 50 IP adresses in less than 0.05 seconds... while MySQL uses about 19 seconds....
Humm, Does that lead us to a question, why would MySQL be that slow... doesn't that mean someday we should use some other kind of database because it is ULTRA fast?

Code:
 <?php
if (!defined("IPFILE")) define ("IPFILE", "QQWry.dat");

function bin2dec($strBin) {
     $intLen = strlen($strBin);
     for (
         $i = 0, $intBase = 1, $intResult = 0;
         $i < $intLen; $i++, $intBase *= 256
     ) $intResult += ord($strBin{$i}) * $intBase;
     return $intResult;
 }

 // error code: 1-open file error; 2-data error;
 function ip2addr($intIp) {
    $arrUnknown = array(
         "region" => "(unknown)",
         "address" => "(unknown)"
     );
     $fileIp = fopen(IPFILE, "rb");
     if (!$fileIp) return 1;
     $strBuf = fread($fileIp, 4);
     $intFirstRecord = bin2dec($strBuf);
     $strBuf = fread($fileIp, 4);
     $intLastRecord = bin2dec($strBuf);
     $intCount = floor(($intLastRecord - $intFirstRecord) / 7);
     if ($intCount < 1) return 2;
     $intStart = 0;
     $intEnd = $intCount;
     while ($intStart < $intEnd - 1) {
         $intMid = floor(($intStart + $intEnd) / 2);
         $intOffset = $intFirstRecord + $intMid * 7;
         fseek($fileIp, $intOffset);
         $strBuf = fread($fileIp, 4);
         $intMidStartIp = bin2dec($strBuf);
         if ($intIp == $intMidStartIp) {
             $intStart = $intMid;
             break;
         }
         if ($intIp > $intMidStartIp) $intStart = $intMid;
         else $intEnd = $intMid;
     }
     $intOffset = $intFirstRecord + $intStart * 7;
     fseek($fileIp, $intOffset);
     $strBuf = fread($fileIp, 4);
     $intStartIp = bin2dec($strBuf);
     $strBuf = fread($fileIp, 3);
     $intOffset = bin2dec($strBuf);
     fseek($fileIp, $intOffset);
     $strBuf = fread($fileIp, 4);
     $intEndIp = bin2dec($strBuf);
     if ($intIp < $intStartIp || $intIp > $intEndIp) return $arrUnknown;
     $intOffset += 4;
     while (($intFlag = ord(fgetc($fileIp))) == 1) {
         $strBuf = fread($fileIp, 3);
         $intOffset = bin2dec($strBuf);
         if ($intOffset < 12) return $arrUnknown;
         fseek($fileIp, $intOffset);
     }
     switch ($intFlag) {
         case 0:
             return $arrUnknown;
             break;
         case 2:
             $intOffsetAddr = $intOffset + 4;
             $strBuf = fread($fileIp, 3);
             $intOffset = bin2dec($strBuf);
             if ($intOffset < 12) return $arrUnknown;
             fseek($fileIp, $intOffset);
             while (($intFlag = ord(fgetc($fileIp))) == 2 || $intFlag == 1) {
                 $strBuf = fread($fileIp, 3);
                 $intOffset = bin2dec($strBuf);                 
if ($intOffset < 12) return $arrUnknown;
                fseek($fileIp, $intOffset);
             }
             if (!$intFlag) return $arrUnknown;
             $arrAddr = array(
                 "region" => chr($intFlag)
             );
             while (ord($c = fgetc($fileIp))) $arrAddr["region"] .= $c;
             fseek($fileIp, $intOffsetAddr);
             while (($intFlag = ord(fgetc($fileIp))) == 2 || $intFlag == 1) {
                 $strBuf = fread($fileIp, 3);
                 $intOffset = bin2dec($strBuf);
                 if ($intOffset < 12) {
                     $arrAddr["address"] = "(unknown)";
                     return $arrAddr;
                 }
                 fseek($fileIp, $intOffset);
             }
             if (!$intFlag) {
                 $arrAddr["address"] = "(unknown)";
                 return $arrAddr;
             }
             $arrAddr["address"] = chr($intFlag);
             while (ord($c = fgetc($fileIp))) $arrAddr["address"] .= $c;
             return $arrAddr;
             break;
         default:
             $arrAddr = array("region" => chr($intFlag));
             while (ord($c = fgetc($fileIp))) $arrAddr["region"] .= $c;
             while (($intFlag = ord(fgetc($fileIp))) == 2 || $intFlag == 1) {
                 $strBuf = fread($fileIp, 3);
                 $intOffset = bin2dec($strBuf);
                 if ($intOffset < 12) {
                     $arrAddr["address"] = "(unknown)";
                     return $arrAddr;
                 }
                 fseek($fileIp, $intOffset);
             }
             if (!$intFlag) {
                 $arrAddr["address"] = "(unknown)";
                 return $arrAddr;
             }
             $arrAddr["address"] = chr($intFlag);
             while (ord($c = fgetc($fileIp))) $arrAddr["address"] .= $c;
             return $arrAddr;
     }
}
?>


also, I used IPLook 1.6
download:www.onlinedown.net/soft/23465.htm
and export the newest QQwry.dat
download"www.skycn.com/soft/14344.html
and now, I got some 12800 KB text file, which looks like this
Code:
StartIP||EndIP||Country||Local
0||16777215||IANA||Unknown
16777216||33554431||IANA||Unknown
33554432||50331647||IANA||Unknown
50331648||67108863||美国||新泽西通用电气公司

I think making an index will speed the whole IP finding thing UP... A LOT...
Related topics
Google Messenger
Best free MMORPG?
New design of the home page
Creating a new Operating System
how to set up a server
Is it a Bug or Easter Egg?
Next generation consoles...
Rules to Make Your Site Annoying
mail system......an mysql....
ip-adress.com
Life in Space
Software Design
Trust Wireless Mouse
Coming out and the people that come with it
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.