网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > PHP > PHP编程代码 > 利用纯真QQIP数据库做快速IP归属地查询  

利用纯真QQIP数据库做快速IP归属地查询

2006-02-27  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

利用纯真版IP数据文件,优化查询方法,提高格式转换效率,减少读取文件的次数,达到快速在线查询IP的目的。大幅度减少文件读取次数,对保护服务器硬盘应该有一定的好处。并增加一个数据库浏览器,可以浏览纯真版QQIP数据文件。


1.用ip2long()做IP校验
2.用sprintf做强制转换
3.用unpack分析二进制数据,减少了 文件读取次数
4.substr strpos分析字符串,这也减少了文件读取次数
5.可惜速度没有明显提高;)


<-form name="ipform" method="get" action=""><-br/>IP Address:<-input type="text" name="ip"/><-br/><-a href="javascript:document.ipform.submit();">Submit<-/a><-br/><-/form>

<?php
define('IPDATA','ipdata.db');
//get ip
$ip = isset($_GET['ip'])?$_GET['ip']:getenv("REMOTE_ADDR");
echo "IP: ARIN";
if (-1 === ($ip = ip2long($ip)))  die("Invalid IP");
$ip = sprintf("%u",$ip);
$country = $city = ''; 网管bitscn_com
echo "
Record No:".Whois($ip,$country,$city);
echo "
Location:  []";

function Whois($ip,&$country,&$city)
{
  //open ip-database
  if (NULL == ($fp = fopen(IPDATA, "rb"))) die("cannot open dbfile.");
  //get data section offset
  $offset = unpack("Vbegin/Vend", fread($fp, 8));
  $begin = 0;$end = ($offset['end'] - $offset['begin']) / 7;
  //seek index
  while ($begin < $end - 1) {
    $num = ($begin + $end) >> 1;
    fseek ($fp , ($offset['begin'] + $num * 7) , SEEK_SET ) ;
    $record = unpack("Vip", fread($fp, 4));
    $record['ip'] = sprintf("%u",$record['ip']);
    if ($ip == $record['ip']) { $begin = $num; break;}
    if ($ip > $record['ip'])    $begin = $num;

网管联盟bitsCN@com


    else                        $end = $num;    
  }
  //read index
  $record = unpack("Vbeginip/Vloc", ReadBinary($fp,$offset['begin']+$begin*7) );
  $locoff =($record['loc'] & 0x00FFFFFF);
  $info = unpack("Vendip/Vloc",ReadBinary($fp,$locoff)) ;
  $info['endip'] = sprintf("%u",$info['endip']);
  if($info['endip'] < $ip)  die(" [unkown location]");
  //get GEO
  $countryoff = (($info['loc'] & 0x000000FF)== 1)?($info['loc'] >> 8):($locoff+4);
  $country = ReadGEOStr($fp,$countryoff);
  $city = ReadGEOStr($fp,$countryoff);
  return $begin;
}  

function ReadGEOStr($fp,&$offset)

网管bitscn_com


{
  $binarydata = ReadBinary($fp,$offset);
  $info = unpack("Vloc",$binarydata);
  if( ($info['loc'] & 0x000000FF) == 2 ) {
    $GeoStr = ReadBinary($fp,$info['loc'] >> 8 );
    $GeoStr = substr($GeoStr,0,strpos($GeoStr, 0));
    $offset += 4;
  }else {
    $len = strpos($binarydata, 0);
    $GeoStr = substr($binarydata,0,$len);
    $offset += $len + 1;
  }
  return $GeoStr;
}  

function ReadBinary($fp,$positionz)
{
  fseek ( $fp , $positionz , SEEK_SET ) ;
  return fread ( $fp , 32 ) ;
}
?>
===================================

2005-02-22
cnss格式QQ数据库
集成PHP浏览查询器
集成官方whois查询 http://www.mydot.org/t539.html
网管联盟bitsCN_com

<form action="" method="get" name="ipform">
IP Address:<input name="ip" type="text">
<a href="javascript:document.ipform.submit();">Submit</a>
</form>

<form action="" method="get" name="record">
Start<input name="s" type="text">
Count<input name="c" type="text">
<a href="javascript:document.record.submit();">Submit</a>
</form>

<?php
define('IPDATA','ipdata.db');

$fp = NULL;
if( isset($_GET['ip']) )
{
  $ipx = $_GET['ip'];
  if (-1 === ($ip = ip2long($ipx)))  die("Invalid IP");
  echo "IP: <a href=\"whoisip.php?ip=\">ARIN</a>";
  $ip = sprintf("%u",$ip);
  $ippostion = ''; 中国网管联盟bitsCN.com
  $start_time=get_time();
  echo "<br>Record No:".Whois($ippostion);
  $end_time=get_time();
  echo "<br>Location:";
  echo "<br>Process Time:".round($end_time-$start_time,3)."seconds";
}
else if ( isset($_GET['s']) )
{
  $first = $_GET['s'];
  $rcount = $_GET['c'];
  $startoff = $end = 0;
  GetSection($startoff,$end);
    
  if( ($i = $first+$rcount)>$end  || $first<0)
    die ("count overflow");
  else
  {
    $j = $first-$rcount;
    echo "<a href='?s=$j&c=$rcount'>Prev</a>:::";
    $j = $first+$rcount;
    echo "<a href='?s=$j&c=$rcount'>Next</a><br>";
网管下载dl.bitscn.com

  }
    
  while( $first < $i)
  {
    echo ReadRecord($startoff,$first,$startip,$endip);
    $startip = long2ip($startip);
    $endip = long2ip($endip);
    echo "&nbsp;:--<br>";
    $first ++;
  }
  fclose($fp);
}
else
{
   echo "Powered by <a href='http://www.upsdn.net'>upsdn.net</a>";
   echo "<br>Thanks to <a href='http://www.cz88.net'>cz88.net</a>";
}  



function Whois(&$country)
{
  global $ip,$fp;

  $startoff = $begin = $end = 0;
  GetSection($startoff,$end);
  //seek index
  while ($begin < $end - 1) {
    $num = ($begin + $end) >> 1; 中国网管论坛bbs.bitsCN.com
    fseek ($fp , $startoff + $num * 7 , SEEK_SET ) ;
    $record = unpack("Vip", fread($fp, 4));
    $record['ip'] = sprintf("%u",$record['ip']);
    if ($ip == $record['ip']) { $begin = $num; break;}
    if ($ip > $record['ip'])    $begin = $num;
    else                        $end = $num;    
  }
  //read index
  $startip = $endip = 0;
  $country = ReadRecord($startoff,$begin,$startip,$endip);
  fclose($fp);
  return $begin;
}  

function GetSection(&$startoff,&$total)
{
  global $fp;
  //open ip-database
  if (NULL == ($fp = fopen(IPDATA, "rb"))) die("cannot open dbfile."); 网管论坛bbs_bitsCN_com
  //get data section offset
  $offset = unpack("Vbegin/Vend", fread($fp, 8));
  $total = ($offset['end'] - $offset['begin']) / 7;
  $startoff = $offset['begin'];
}

function ReadRecord($startoff,$number,&$startip,&$endip)
{
  global $ip;
  $record = unpack("Vbeginip/Vloc", ReadBinary($startoff+$number*7) );
  $startip = $record['beginip'];
  $locoff =($record['loc'] & 0x00FFFFFF);
  $info = unpack("Vendip/Vloc",ReadBinary($locoff)) ;
  $endip = $info['endip'];
  $info['endip'] = sprintf("%u",$info['endip']);
  if($info['endip'] < $ip)  die(" [unkown location]");
  //get GEO
  $countryoff = (($info['loc'] & 0x000000FF)== 1)?($info['loc'] >> 8):($locoff+4);
  return ReadGEOStr($countryoff)."->".ReadGEOStr($countryoff);    

网管网www_bitscn_com


}

function ReadGEOStr(&$offset)
{
  $binarydata = ReadBinary($offset);
  $info = unpack("Vloc",$binarydata);
  if( ($info['loc'] & 0x000000FF) == 2 ) {
    $GeoStr = ReadBinary($info['loc'] >> 8 );
    $GeoStr = substr($GeoStr,0,strpos($GeoStr, 0));
    $offset += 4;
  }else {
    $len = strpos($binarydata, 0);
    $GeoStr = substr($binarydata,0,$len);
    $offset += $len + 1;
  }
  return $GeoStr;
}  

function ReadBinary($positionz)
{
  global $fp;
  fseek ( $fp , $positionz , SEEK_SET ) ;
  return fread ( $fp , 32 ) ;
}

function get_time() {
  $mtime=microtime();
  $mtime=explode(" ",$mtime); 网管下载dl.bitscn.com
  $mtime=$mtime[1]+$mtime[0];
  return($mtime);
}
?>

纯真版IP数据库下载:http://www.cz88.net/ip/

TAGs
 上一篇:用PHP查询域名状态whois的类   下一篇:用PHP实现标准的IP Whois查询
利用纯真QQIP数据库做快速IP归属地查询 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: