PHP:根據IP地址擷取所在城市

來源:互聯網
上載者:User
檔案目錄: ipLocation-----qqwry----------QQWry.Dat-----ipCity.class.php  ipCity.class.php檔案代碼:  
<?php    class ipCity {        /**      * 根據ip地址擷取對應所在城市      * @param type $userip 使用者IP地址      * @return string      */      public function getCity( $userip, $dat_path = '' ) {          //IP資料庫路徑,這裡用的是QQ IP資料庫 20110405 純真版          empty( $dat_path ) && $dat_path = FCPATH . 'plugin/ipLocation/qqwry/QQWry.Dat';          //判斷IP地址是否有效          if ( preg_match( "/^([0-9]{1,3}.){3}[0-9]{1,3}$/", $userip ) == 0 ) {              return 'IP Address Invalid';          }          //開啟IP資料庫          if ( !$fd = @fopen( $dat_path, 'rb' ) ) {              return 'IP data file not exists or access denied';          }          //explode函數分解IP地址,運算得出整數形結果          $userip = explode( '.', $userip );          $useripNum = $userip[0] * 16777216 + $userip[1] * 65536 + $userip[2] * 256 + $userip[3];          //擷取IP地址索引開始和結束位置          $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;          //使用二分尋找法從索引記錄中搜尋匹配的IP地址記錄          while ( $userip1num > $useripNum  $userip2num < $useripNum ) {              $Middle = intval( ($EndNum + $BeginNum) / 2 );              //位移指標到索引位置讀取4個位元組              fseek( $fd, $useripbegin + 7 * $Middle );              $useripData1 = fread( $fd, 4 );              if ( strlen( $useripData1 ) < 4 ) {                  fclose( $fd );                  return 'File Error';              }              //提取出來的資料轉換成長整形,如果資料是負數則加上2的32次冪              $userip1num = implode( '', unpack( 'L', $useripData1 ) );              if ( $userip1num < 0 )                  $userip1num += pow( 2, 32 );              //提取的長整型數大於我們IP地址則修改結束位置進行下一次迴圈              if ( $userip1num > $useripNum ) {                  $EndNum = $Middle;                  continue;              }              //取完上一個索引後取下一個索引              $DataSeek = fread( $fd, 3 );              if ( strlen( $DataSeek ) < 3 ) {                  fclose( $fd );                  return 'File Error';              }              $DataSeek = implode( '', unpack( 'L', $DataSeek . chr( 0 ) ) );              fseek( $fd, $DataSeek );              $useripData2 = fread( $fd, 4 );              if ( strlen( $useripData2 ) < 4 ) {                  fclose( $fd );                  return 'File Error';              }              $userip2num = implode( '', unpack( 'L', $useripData2 ) );              if ( $userip2num < 0 )                  $userip2num += pow( 2, 32 );              //找不到IP地址對應城市              if ( $userip2num < $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 ) ) {                  $AddrSeek2 = fread( $fd, 3 );                  if ( strlen( $AddrSeek2 ) < 3 ) {                      fclose( $fd );                      return 'System Error';                  }                  $AddrSeek2 = implode( '', unpack( 'L', $AddrSeek2 . chr( 0 ) ) );                  fseek( $fd, $AddrSeek2 );              } else {                  fseek( $fd, -1, SEEK_CUR );              }              while ( ($char = fread( $fd, 1 )) != chr( 0 ) )                  $useripAddr2 .= $char;              $AddrSeek = implode( '', unpack( 'L', $AddrSeek . chr( 0 ) ) );              fseek( $fd, $AddrSeek );              while ( ($char = fread( $fd, 1 )) != chr( 0 ) )                  $useripAddr1 .= $char;          } else {              fseek( $fd, -1, SEEK_CUR );              while ( ($char = fread( $fd, 1 )) != chr( 0 ) )                  $useripAddr1 .= $char;              $useripFlag = fread( $fd, 1 );              if ( $useripFlag == chr( 2 ) ) {                  $AddrSeek2 = fread( $fd, 3 );                  if ( strlen( $AddrSeek2 ) < 3 ) {                      fclose( $fd );                      return 'System Error';                  }                  $AddrSeek2 = implode( '', unpack( 'L', $AddrSeek2 . chr( 0 ) ) );                  fseek( $fd, $AddrSeek2 );              } else {                  fseek( $fd, -1, SEEK_CUR );              }              while ( ($char = fread( $fd, 1 )) != chr( 0 ) ) {                  $useripAddr2 .= $char;              }          }          fclose( $fd );          //返回IP地址對應的城市結果          if ( preg_match( '/http/i', $useripAddr2 ) ) {              $useripAddr2 = '';          }          $useripaddr = "$useripAddr1 $useripAddr2";          $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;      }        /**      * 判斷是否我utf-8編碼的字串      * @param type $string      * @return boolean      */      private function 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檔案可以在百度上搜尋或者需要的發你郵箱來,我通過郵箱發給你。 使用示範:  
include FCPATH . 'plugin/ipLocation/ipCity.class.php';  $city = new ipCity();  $addr = $city->getCity( '172.0.0.1' );  echo $addr; // echo 本地地址  
 

相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。