PHP: Get the city by IP address

Source: Internet
Author: User
Tags chr explode fread php class php file string strlen unpack
File directory:   iplocation-----qqwry----------QQWry.Dat-----ipCity.class.php     ipCity.class.php file code:    
<?php class IpCity {/** * based on IP address to obtain the corresponding city * @param type $userip User IP address * @return Strin  
        G */Public function getcity ($userip, $dat _path = ') {//IP database path, here is the QQ IP database 20110405 Pure version Empty ($dat _path) && $dat _path = Fcpath.  
        ' Plugin/iplocation/qqwry/qqwry.dat '; Determine if the IP address is valid if (Preg_match ("/^" ([0-9]{1,3}.) {3} [0-9]  
        {1,3}$/", $userip) = = 0) {return ' IP address Invalid ';  //Open IP database if (! $fd = @fopen ($dat _path, ' RB ')) {return ' IP data file not exists or  
        Access Denied ';  
        The//explode function decomposes the IP address, and the result of the integer shape is calculated $userip = Explode ('. ', $userip);  
        $useripNum = $userip [0] * 16777216 + $userip [1] * 65536 + $userip [2] * 256 + $userip [3];  
        Gets the IP address index start and end position $DataBegin = Fread ($FD, 4);  
        $DataEnd = Fread ($FD, 4);  $useripbegin = Implode (', Unpack (' L ', $DataBegin));
        if ($useripbegin < 0) $useripbegin + = POW (2, 32);  
        $useripend = Implode (', Unpack (' L ', $DataEnd));  
        if ($useripend < 0) $useripend + = POW (2, 32);  
        $useripAllNum = ($useripend-$useripbegin)/7 + 1;  
        $BeginNum = 0;  
        $EndNum = $useripAllNum; Use the binary lookup method to search for a matching IP address record from the index record while ($userip 1num > $useripNum $userip 2num < $useripNum) {$M  
            Iddle = Intval (($EndNum + $BeginNum)/2);  
            The offset pointer reads 4 bytes to the index position fseek ($FD, $useripbegin + 7 * $Middle);  
            $useripData 1 = fread ($FD, 4);  
                if (strlen ($useripData 1) < 4) {fclose ($FD);  
            Return ' File Error ';  
            ///Extract Data conversion growth shaping, if the data is negative then add 2 of the 32 power $userip 1num = implode (', Unpack (' L ', $useripData 1)); if ($userip 1num < 0) $userip 1num + + POW (2, 32);  
            The number of long integers fetched is greater than our IP address to modify the end position for the next loop if ($userip 1num > $useripNum) {$EndNu  
                m = $Middle;  
            Continue  
            After the previous index is taken, remove an index $DataSeek = fread ($FD, 3);  
                if (strlen ($DataSeek) < 3) {fclose ($FD);  
            Return ' File Error ';  
            $DataSeek = Implode (', Unpack (' L ', $DataSeek. chr (0)));  
            Fseek ($FD, $DataSeek);  
            $useripData 2 = fread ($FD, 4);  
                if (strlen ($useripData 2) < 4) {fclose ($FD);  
            Return ' File Error ';  
            $userip 2num = Implode (', Unpack (' L ', $useripData 2));  
            if ($userip 2num < 0) $userip 2num + + POW (2, 32);  
                 Could not find IP address corresponding city if ($userip 2num < $useripNum) {if ($Middle = = $BeginNum) {   Fclose ($FD);  
                Return ' No Data ';  
            } $BeginNum = $Middle;  
        }} $useripFlag = Fread ($FD, 1);  
            if ($useripFlag = = Chr (1)) {$useripSeek = Fread ($FD, 3);  
                if (strlen ($useripSeek) < 3) {fclose ($FD);  
            Return ' System Error ';  
            $useripSeek = Implode (', Unpack (' L ', $useripSeek. chr (0)));  
            Fseek ($FD, $useripSeek);  
        $useripFlag = Fread ($FD, 1);  
            } if ($useripFlag = = Chr (2)) {$AddrSeek = Fread ($FD, 3);  
                if (strlen ($AddrSeek) < 3) {fclose ($FD);  
            Return ' System Error ';  
            $useripFlag = Fread ($FD, 1);  
                if ($useripFlag = = Chr (2)) {$AddrSeek 2 = fread ($FD, 3); if (strlen ($AddrSeEK2) < 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)) $useripAddr 2. = $char;  
            $AddrSeek = Implode (', Unpack (' L ', $AddrSeek. chr (0)));  
            Fseek ($FD, $AddrSeek);  
        while ($char = Fread ($FD, 1))!= chr (0)) $useripAddr 1. = $char;  
            else {fseek ($FD,-1, seek_cur);  
            while ($char = Fread ($FD, 1))!= chr (0)) $useripAddr 1. = $char;  
            $useripFlag = Fread ($FD, 1);  
                if ($useripFlag = = 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)) {$useripAddr 2. = $char;  
        } fclose ($FD);  
        Returns the city result of the IP address if (preg_match ('/http/i ', $useripAddr 2)) {$useripAddr 2 = ';  
        $USERIPADDR = "$useripAddr 1 $useripAddr 2";  
        $USERIPADDR = preg_replace ('/cz88.net/is ', ', ', $useripaddr);  
        $USERIPADDR = preg_replace ('/^s*/is ', ', ', $useripaddr);  
        $USERIPADDR = preg_replace ('/s*$/is ', ', ', $useripaddr);  
       if (Preg_match ('/http/i ', $useripaddr) $useripaddr = = ') {$useripaddr = ' No Data '; } elseif (! $this->is_utf8 ($useripaddr)) {$useripaddr = Iconv (' GBK ', ' UTF-8 ', $useripaddr);  
    return $useripaddr; /** * To determine if I utf-8 encoded string * @param type $string * @return Boolean/private Functio n Is_utf8 ($string) {if (Preg_match) ("/^". Chr (228). "-" . Chr (233). "]{1}[". Chr (128). "-" . Chr (191). "]{1}[". Chr (128). "-" . Chr (191). ']{1} ' {1}/', $string) = = True Preg_match ("/". Chr (228). "-" . Chr (233). "]{1}[". Chr (128). "-" . Chr (191). "]{1}[". Chr (128). "-" . Chr (191). ']{1} ' {1}$/', $string) = = True Preg_match ("/". Chr (228). "-" . Chr (233). "]{1}[". Chr (128). "-" . Chr (191). "]{1}[". Chr (128). "-" . Chr (191).  
        ']{1} ' {2,}/', $string) = = True) {return true;  
        else {return false;   }  
    }  
  
}
QQWry.Dat files can be searched on Baidu or need to send your mailbox, I sent you through the mailbox. Use Demo:
Include Fcpath. ' plugin/iplocation/ipcity.class.php ';  
$city = new IpCity ();  
$ADDR = $city->getcity (' 172.0.0.1 ');  
Echo $addr; Echo Local Address  


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.