Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17757874
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类:

2008-04-17 17:37:14

 /**********************************************************************
* ip 来源追踪 ver 1.1a
* 作者 耙子 pazee@21cn.com
* 2002/08/10
*
* 程序中的来自《追捕》,请把追捕中wry.dll 拷贝到函数当前目录。
* 追捕的数据库是个的dbf文件,只不过它的扩展名字变成了dll。
* 2000年我在一个偶然的机会发现了他的dll的真实格式,后来的很多文章也提到了此格式,
* 《追捕》的数据文件目前应用非常广泛,很多查ip来源程序的基本都用到了他的数据库。
* 比如有个去广告,能显示ip来源的qq就使用了他。
* 2001年初写过一个php的函数,但是需要php的dbf模块支持,很多网站并不提供此模块。
* 现在的版本采用二进制的文件读写,不依赖php的dbf的支持了,没有用到
* 任何shell命令.
* 由于数据文件本身是有序的,所以非常方便的采用了折半查找的算法,
* 速度很快,目前的20020325版本的数据库,大约有记录28905条,最多比较14次。
*
* 在此感谢《追捕》作者“冯志宏”
* 有任何问题请于我联系,谢谢! pazee@21cn.com
*
*
* 声明:
* 你可以随意传播、复制、修改此程序,但是请保留此段文字。
* 请勿用在商业软件上、请勿用在不正当的地方(这是《追捕》的要求),
* 再次表示谢谢。
***********************************************************************/

// define path of wry.dll
define("dbfilename", "wry.dll");


class trec
{
var $startip;
var $endip;
var $country;
var $local;
}

class twru
{
var $ip;
var $fp;
var $rec;
var $datafieldbegin= 0xc2;
var $recordlength;

// check ip and format ip
function formatip($ip)
{
$ret= ereg("^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$", $ip, $ipsection);
if ($ret == false)
return -1; // invild ip
$this->ip= '';
for ($i=1; $i<=4; $i++)
if ($ipsection[$i] > 255)
return -1;
else
$this->ip.= sprintf("%03.0f", $ipsection[$i]). (($i<4) ? '.' : '');
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 <= 1)
{
$szlocal= "filedataerror";
$nret= 2;
}
else
{
$rangb= 0;
$range= $recordcount;
// match ...
while ($rangb < $range-1)
{
$recno= floor(($rangb + $range) / 2);
$this->readrec($recno);
if (strcmp($this->ip, $this->rec->startip) >=0 && strcmp($this->ip, $this->rec->endip) <=0 )
break; //found match record
if (strcmp($this->ip, $this->rec->startip) > 0)
$rangb= $recno;
else
$range= $recno;
}
if (!($rangb < $range-1))
{
$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的自然数。
* ********************************************************************/

// below, it is test code.
$wru= new twru;
$szresult="";
$ip= "202.96.134.133";
// $ip= $remote_addr;
$wru->wru($ip, $szresult);
echo $ip."
";
echo $szresult;
//---------------------------------------------------------------------------
?>

阅读(372) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~