PHP 根據IP位址獲取所在城市

2022-04-08 18:53:11 字數 4162 閱讀 1715

檔案目錄:

iplocation

-----qqwry

----------qqwry.dat

-----ipcity.class.php  

ipcity.class.php檔案**:

<?php class ipcity .)[0-9]$/", $userip ) == 0 )

//開啟ip資料庫

if ( !$fd = @fopen( $dat_path, 'rb' ) )

//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 )

//提取出來的資料轉換成長整形,如果資料是負數則加上2的32次冪

$userip1num = implode( '', unpack( 'l', $useripdata1 ) );

if ( $userip1num < 0 )

$userip1num += pow( 2, 32 );

//提取的長整型數大於我們ip位址則修改結束位置進行下一次迴圈

if ( $userip1num > $useripnum )

//取完上乙個索引後取下乙個索引

$dataseek = fread( $fd, 3 );

if ( strlen( $dataseek ) < 3 )

$dataseek = implode( '', unpack( 'l', $dataseek . chr( 0 ) ) );

fseek( $fd, $dataseek );

$useripdata2 = fread( $fd, 4 );

if ( strlen( $useripdata2 ) < 4 )

$userip2num = implode( '', unpack( 'l', $useripdata2 ) );

if ( $userip2num < 0 )

$userip2num += pow( 2, 32 );

//找不到ip位址對應城市

if ( $userip2num < $useripnum )

$beginnum = $middle;

} }

$useripflag = fread( $fd, 1 );

if ( $useripflag == chr( 1 ) )

$useripseek = implode( '', unpack( 'l', $useripseek . chr( 0 ) ) );

fseek( $fd, $useripseek );

$useripflag = fread( $fd, 1 );

} if ( $useripflag == chr( 2 ) )

$useripflag = fread( $fd, 1 );

if ( $useripflag == chr( 2 ) )

$addrseek2 = implode( '', unpack( 'l', $addrseek2 . chr( 0 ) ) );

fseek( $fd, $addrseek2 );

} else

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

$addrseek2 = implode( '', unpack( 'l', $addrseek2 . chr( 0 ) ) );

fseek( $fd, $addrseek2 );

} else

while ( ($char = fread( $fd, 1 )) != chr( 0 ) )

} fclose( $fd );

//返回ip位址對應的城市結果

* 判斷是否我utf-8編碼的字串

* @param type $string

* @return boolean

*/

private function is_utf8( $string ) [" . chr( 128 ) . "-" . chr( 191 ) . "][" . chr( 128 ) . "-" . chr( 191 ) . "])/", $string ) == true || preg_match( "/([" . chr( 228 ) . "-" . chr( 233 ) . "][" . chr( 128 ) . "-" . chr( 191 ) . "][" . chr( 128 ) . "-" . chr( 191 ) . "])$/", $string ) == true || preg_match( "/([" . chr( 228 ) . "-" . chr( 233 ) . "][" . chr( 128 ) . "-" . chr( 191 ) . "][" . chr( 128 ) . "-" . chr( 191 ) . "])/", $string ) == true ) else

} }  

qqwry.dat檔案可以在百度上搜尋或者需要的發你郵箱來,我通過郵箱發給你。

使用演示:

include fcpath . 'plugin/iplocation/ipcity.class.php';

$city = new ipcity();

$addr = $city->getcity( '172.0.0.1' );

echo $addr; // echo 本地位址

取IP位址所在城市

using system using system.collections.generic using system.linq using system.web using system.io using system.text using system.text.regularexpression...

根據IP定位使用者所在城市資訊

即可返回當前ip所在的省市資訊。本地js呼叫 var myprovince remote ip info province var mycity remote ip info city var mydistrict remote ip info district function html 為 您所...

根據IP定位使用者所在城市資訊

當我們在頁面中填寫個人資料中的省市資訊時,頁面會根據當前使用者所在的城市自動選中省市下拉選項 當我們訪問某個大型站點時,程式會自動進入到使用者當前所在的城市頻道。這些功能是如何實現的呢?今天我們來通過例項講解如何根據ip定位使用者所在城市資訊。根據當前使用者本地ip位址,查詢ip位址庫,獲取ip所在...