p>《追捕》现在已经响当当了,他的成功,在于它的数据库的维护的毅力,
很多软件都在用它的数据,有些改了名字,变成xxx.dat了,嗬嗬,我觉得不太可取,有点儿不地道的感觉。
我的代码也用到了他,改不改名字无所谓了,关键尊重人家的劳动就好了。
在网页上增加一个段简单的代码,就可以显示来访者的地点。
还可以在自己的网站里面写一个个性化的日志,记录ip也记录地点。
效果可以查看 www.fogsun.com 的左边下角.
ip= ';
for ($i=1; $i if ($ipsection[$i] > 255)
return -1;
else
$this->ip.= sprintf(%03.0f, $ipsection[$i]). (($i return 0;
}
// read a record from db
function readrec($recno)
{
$this->seek($recno);
$buf= fread($this->fp, $this->recordlength);
if (strlen($buf) == 0)
{
return 1;
}
$this->rec->startip= (substr($buf, 0, 17));
$this->rec->endip= trim(substr($buf, 17, 22));
$this->rec->country= trim(substr($buf, 17+22, 13));
$this->rec->local= trim(substr($buf, 17+22+13, 47));
return 0;
}
// go to record number
function seek($recno)
{
return fseek($this->fp, $recno * $this->recordlength + $this->datafieldbegin, seek_set);
}
// where_are_you main fucntion
/*********************************************
* 使用说明
* 参数:
* ip 合法ip地址即可
* szlocal 是保存返回的结果字符串的
* 返回值:
* 此函数有返回值,可以根据返回值自行处理结果
* 0: 查找成功
* -1: 无效的ip
* 1: 打开数据库文件失败
* 2: 数据文件错误(没找到有效记录)
* 3: 未知 ip
**********************************************/
function wru($ip, &$szlocal)
{
$this->rec= new trec;
$nret= 0;
$this->recordlength= 17 + 22 + 13 + 47 + 12 + 1;
if ($this->formatip($ip) != 0)
{
$szlocal= invalidip;
return -1;
}
$this->fp= fopen(dbfilename, rb);
if ($this->fp == null) {
$szlocal= openfileerror;
return 1;
}
// get record count
fseek($this->fp, 0, seek_end);
$recordcount= floor((ftell($this->fp) - $this->datafieldbegin) / $this->recordlength);
if ($recordcount {
$szlocal= filedataerror;
$nret= 2;
}
else
{
$rangb= 0;
$range= $recordcount;
// match ...
while ($rangb {
$recno= floor(($rangb + $range) / 2);
$this->readrec($recno);
if (strcmp($this->ip, $this->rec->startip) >=0 && strcmp($this->ip, $this->rec->endip) break; //found match record
if (strcmp($this->ip, $this->rec->startip) > 0)
$rangb= $recno;
else
$range= $recno;
}
if (!($rangb {
$szlocal= unknowlocal!;
$nret= 3;
}
else
{ // match success
$szlocal= $this->rec->country;
$szlocal.= $this->rec->local;
}
}
fclose($this->fp);
return $nret;
}
}
/*******************************************************************
* 变更记录:
* 2002/08/10 完成版本 1.0a
* 2002/08/12 增加formatip成员函数,提供了对ip的标准格式化,支持
* 202.96.128.68 这类的写法,类的内部自动转为 202.096.128.068,
* 同时提供了完整的对ip地址的有效检查。规则是4个整数部分均不超
* 过255的自然数。
* ********************************************************************/
?>
// test code.
$wru= new twru;
$szresult=;
$ip= 202.96.134.133;
// $ip= $remote_addr;
$wru->wru($ip, $szresult);
echo $ip.
;
echo $szresult;
//---------------------------------------------------------------------------
?>