- 论坛徽章:
- 0
|
如何使用PHP来操纵dbf数据库文件
看看这个你就明白了!
- <?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("DBFILENAME", "ipwry.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的自然数。
- * ********************************************************************/
- ?>;
- <?
- // Test Code.
- $wru= new TWru;
- $szResult="";
- $ip=$_POST["ip"];
- $wru->;wru($ip, $szResult);
- print("<form name=form1 action=".$_SERVER["PHP_SELF"]." method=post>;");
- print("<div align=center>;<input type=text name=ip>;</div>;<br>;");
- print("<div align=center>;<input type=submit value=给我找>;<input type=reset value=输错了>;</div>;");
- print("</form>;");
- print("<div align=center>; $ip </div>;<br>;");
- print("<div align=center>; 这个ip地址来自于: $szResult</div>;<br>;");
-
- //---------------------------------------------------------------------------
- ?>;
复制代码 |
|