PHP calls pure IP database to return specific address

Source: Internet
Author: User
Tags fread pow unpack

Tag:while    Installation complete    fclose   dat file     preparation     ott    after completion    san    data    

    function Convertip ($ip) {$ip 1num = 0;        $ip 2num = 0;        $ipAddr 1 = "";        $ipAddr 2 = "";                $dat _path = './qqwry.dat ';         if (!preg_match ("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {return ' IP Address Error ';         } 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] * + + $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 ';       } return $ipaddr;   }

Just call this method, pass in a correct IP address, will be able to get a pure IP database in the corresponding geographical location

For example:
Convertip (202.96.209.5)  
The value returned is: Shanghai TelecomNote: Files written with this method need to be placed in the same directory as the pure database file (QQWry.Dat), otherwise the content of $dat_path will need to be modified.

Preparatory work:

Recommended local IP Address database, please go to http://www.cz88.net/this website to download a pure IP database, after installation, to the installation directory of the QQWry.dat files out, this is the IP database we want, put it in the directory you want.

PHP calls pure IP database to return specific address

Related Article

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.