选择显示字体大小

web追捕 php版 源代码


   <?php
/**********************************************************************
* ip 来源追踪 ver 1.1a
* 作者 耙子 pazee@21cn.com http://www.fogsun.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(&quot;dbfilename&quot;, &quot;wry.dll&quot;);


class trec
{
var &#36;startip;
var &#36;endip;
var &#36;country;
var &#36;local;
}

class twru
{
var &#36;ip;
var &#36;fp;
var &#36;rec;
var &#36;datafieldbegin= 0xc2;
var &#36;recordlength;

// check ip and format ip
function formatip(&#36;ip)
{
&#36;ret= ereg(&quot;^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)&#36;&quot;, &#36;ip, &#36;ipsection);
if (&#36;ret == false)
return -1; // invild ip
&#36;this->ip= &#39;&#39;;
for (&#36;i=1; &#36;i<=4; &#36;i++)
if (&#36;ipsection[&#36;i] > 255)
return -1;
else
&#36;this->ip.= sprintf(&quot;%03.0f&quot;, &#36;ipsection[&#36;i]). ((&#36;i<4) ? &#39;.&#39; : &#39;&#39;);
return 0;
}

// read a record from db
function readrec(&#36;recno)
{
&#36;this->seek(&#36;recno);
&#36;buf= fread(&#36;this->fp, &#36;this->recordlength);
if (strlen(&#36;buf) == 0)
{
return 1;
}
&#36;this->rec->startip= (substr(&#36;buf, 0, 17));
&#36;this->rec->endip= trim(substr(&#36;buf, 17, 22));
&#36;this->rec->country= trim(substr(&#36;buf, 17+22, 13));
&#36;this->rec->local= trim(substr(&#36;buf, 17+22+13, 47));
return 0;
}

// go to record number
function seek(&#36;recno)
{
return fseek(&#36;this->fp, &#36;recno * &#36;this->recordlength + &#36;this->datafieldbegin, seek_set);
}

// where_are_you main fucntion
/*********************************************
* 使用说明
* 参数:
* ip 合法ip地址即可
* szlocal 是保存返回的结果字符串的
* 返回值:
* 此函数有返回值,可以根据返回值自行处理结果
* 0: 查找成功
* -1: 无效的ip
* 1: 打开数据库文件失败
* 2: 数据文件错误(没找到有效记录)
* 3: 未知 ip
**********************************************/
function wru(&#36;ip, &&#36;szlocal)
{
&#36;this->rec= new trec;
&#36;nret= 0;
&#36;this->recordlength= 17 + 22 + 13 + 47 + 12 + 1;
if (&#36;this->formatip(&#36;ip) != 0)
{
&#36;szlocal= &quot;invalidip&quot;;
return -1;
}

&#36;this->fp= fopen(dbfilename, &quot;rb&quot;);
if (&#36;this->fp == null) {
&#36;szlocal= &quot;openfileerror&quot;;
return 1;
}

// get record count
fseek(&#36;this->fp, 0, seek_end);
&#36;recordcount= floor((ftell(&#36;this->fp) - &#36;this->datafieldbegin) / &#36;this->recordlength);
if (&#36;recordcount <= 1)
{
&#36;szlocal= &quot;filedataerror&quot;;
&#36;nret= 2;
}
else
{
&#36;rangb= 0;
&#36;range= &#36;recordcount;
// match ...
while (&#36;rangb < &#36;range-1)
{
&#36;recno= floor((&#36;rangb + &#36;range) / 2);
&#36;this->readrec(&#36;recno);
if (strcmp(&#36;this->ip, &#36;this->rec->startip) >=0 && strcmp(&#36;this->ip, &#36;this->rec->endip) <=0 )
break; //found match record
if (strcmp(&#36;this->ip, &#36;this->rec->startip) > 0)
&#36;rangb= &#36;recno;
else
&#36;range= &#36;recno;
}
if (!(&#36;rangb < &#36;range-1))
{
&#36;szlocal= &quot;unknowlocal!&quot;;
&#36;nret= 3;
}
else
{ // match success
&#36;szlocal= &#36;this->rec->country;
&#36;szlocal.= &#36;this->rec->local;
}
}
fclose(&#36;this->fp);
return &#36;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.
&#36;wru= new twru;
&#36;szresult=&quot;&quot;;
&#36;ip= &quot;202.96.134.133&quot;;
// &#36;ip= &#36;remote_addr;
&#36;wru->wru(&#36;ip, &#36;szresult);
echo &#36;ip.&quot;<br>&quot;;
echo &#36;szresult;
//---------------------------------------------------------------------------
?>


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons