PHP:根据IP地址获取所在城市
程序员文章站
2022-04-29 18:07:42
文件目录:
iplocation
-----qqwry
----------qqwry.dat
-----ipcity.class.php
...
文件目录:
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 本地地址