WEB追捕 PHP版 原始碼

來源:互聯網
上載者:User
web|原始碼 <?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", "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."<br>";
echo $szResult;
//---------------------------------------------------------------------------
?>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.