File directory:
ipLocation
-----qqwry
----------QQWry.Dat
-----ipCity.class.php
ipCity.class.php file code:
class ipCity {
/**
* Get the corresponding city based on the ip address
* @param type $userip User IP address
* @return string
*/
public function getCity( $userip, $dat_path = '' ) {
//IP database path, the pure version of QQ IP database 20110405 used here
empty( $dat_path ) && $dat_path = FCPATH . 'plugin/ipLocation/qqwry/QQWry.Dat';
// Determine whether the IP address is valid
1,3}.){3}[0-9]{1,3}$/", $userip ) == 0 ) {
return 'IP Address Invalid';
Open IP database
Decompose the IP address and get an integer result
$userip = explode('.', $userip);
[2] * 256 + $userip[3];
//Get the start and end position of the IP address index
$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 binary search method to search for matching IP addresses from index records Record
while ( $userip1num > $useripNum || $userip2num < $useripNum ) {
$Middle = intval( ($EndNum + $BeginNum) / 2 );
//Offset pointer to Read 4 bytes at the index position
fseek( $fd, $useripbegin + 7 * $Middle );
$useripData1 = fread( $fd, 4 );
if ( strlen( $useripData1 ) < ; 4) {
;幂
$userip1num = implode( '', unpack( 'L', $useripData1 ) );
if ( $userip1num < 0 )
$userip1num += pow( 2, 32 );
// The extraction of the extraction is greater than the IP address of our IP, and the end position is modified for the next cycle. > }
Get the next index fd );
return 'File Error';
}
($fd , $DataSeek ); Return 'File Error ';
}
$userip2num = implode( '', unpack( 'L', $useripData2 ) );
if ( $userip2num < 0 )
$userip2num + =pow(2, 32 );
//The city corresponding to the IP address cannot be found close( $fd );
return 'No Data'; 🎜> $useripFlag = fread( $fd, 1);
if ( $ useripFlag == chr( 1 ) ) {
useripSeek = fread( $fd, 3 ); 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 ) {
$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;
}
/**
* Determine whether my utf-8 encoded string
* @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文件下载地址:http://xiazai.jb51.net/201311/yuanma/qqwry.dat(jb51.net).zip
使用演示: