文件目录:
iplocation
-----qqwry
----------qqwry.dat
-----ipcity.class.php
ipcity.class.php文件代码:
复制代码 代码如下:
$useripnum || $userip2num $middle = intval( ($endnum + $beginnum) / 2 );
//偏移指针到索引位置读取4个字节
fseek( $fd, $useripbegin + 7 * $middle );
$useripdata1 = fread( $fd, 4 );
if ( strlen( $useripdata1 ) fclose( $fd );
return 'file error';
}
//提取出来的数据转换成长整形,如果数据是负数则加上2的32次幂
$userip1num = implode( '', unpack( 'l', $useripdata1 ) );
if ( $userip1num $userip1num += pow( 2, 32 );
//提取的长整型数大于我们ip地址则修改结束位置进行下一次循环
if ( $userip1num > $useripnum ) {
$endnum = $middle;
continue;
}
//取完上一个索引后取下一个索引
$dataseek = fread( $fd, 3 );
if ( strlen( $dataseek ) fclose( $fd );
return 'file error';
}
$dataseek = implode( '', unpack( 'l', $dataseek . chr( 0 ) ) );
fseek( $fd, $dataseek );
$useripdata2 = fread( $fd, 4 );
if ( strlen( $useripdata2 ) fclose( $fd );
return 'file error';
}
$userip2num = implode( '', unpack( 'l', $useripdata2 ) );
if ( $userip2num $userip2num += pow( 2, 32 );
//找不到ip地址对应城市
if ( $userip2num 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 ) 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 ) fclose( $fd );
return 'system error';
}
$useripflag = fread( $fd, 1 );
if ( $useripflag == chr( 2 ) ) {
$addrseek2 = fread( $fd, 3 );
if ( strlen( $addrseek2 ) 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 ) 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文件下载地址:http://xiazai.jb51.net/201311/yuanma/qqwry.dat(jb51.net).zip
使用演示:
复制代码 代码如下:
include fcpath . 'plugin/iplocation/ipcity.class.php';
$city = new ipcity();
$addr = $city->getcity( '172.0.0.1' );
echo $addr; // echo 本地地址
http://www.bkjia.com/phpjc/621713.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/621713.htmltecharticle文件目录: iplocation -----qqwry ----------qqwry.dat -----ipcity.class.php ipcity.class.php文件代码: 复制代码 代码如下: ?php class ipcity { /** * 根据ip地址获...