Web Hunt PHP Version source code

Source: Internet
Author: User
Tags format define copy range return strcmp version trim
web| Source Code <?php
/**********************************************************************
* IP Source Tracking Ver 1.1a
* Author Rake pazee@21cn.com http://www.fogsun.com
* 2002/08/10
*
* The database in the program is from "Hunt", please copy the Wry.dll to the current directory of the function.
* The database being hunted is a DBF file, except that its extended name becomes a DLL.
* 2000 I stumbled upon a chance to discover the real format of his DLL, and many later articles also mentioned this format,
* "Hunt" data files are currently widely used, many of the basic IP source procedures used in his database.
* For example, there is a go to advertising, can display the IP source QQ to use him.
* A PHP function was written in early 2001, but it needs to be supported by the DBF module in PHP, and many websites do not provide this module.
* Now version of the binary file read and write, do not rely on the dbf of PHP support, no use
* Any shell command.
* Because the data file itself is orderly, so it is very convenient to use the binary lookup algorithm,
* Fast, the current 20020325 version of the database, about 28,905 records, up to 14 times.
*
* Thanks to the "chase" author "Feng Zhihong"
* Have any questions please contact me, thank you! Pazee@21cn.com
*
*
Statement
* You can spread, copy, modify this program, but please keep this text.
* Code is not used in commercial software, do not use in improper place (this is "chase" requirements),
* Thank you again.
***********************************************************************/

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
/*********************************************
* Instructions for use
Parameters
* IP Legal IP address can
* Szlocal is the result string that holds the returned
* Return value:
* This function has a return value, you can handle the result based on the return value
* 0: Find success
*-1: Invalid IP
* 1: Failed to open database file
* 2: Data file error (no valid records found)
* 3: Unknown 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-&GT;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-&GT;FP);
return $nRet;
}
}

/*******************************************************************
* Change record:
* 2002/08/10 Complete version 1.0a
* 2002/08/12 Add Formatip member function, provide standard format for IP, support
* 202.96.128.68 This type of writing, the interior of the class is automatically converted to 202.096.128.068,
* At the same time provide a complete IP address of the effective check. The rule is 4 integer parts are not super
* Over 255 of the natural number.
* ********************************************************************/

Below, it is test code.
$WRU = new Twru;
$szResult = "";
$ip = "202.96.134.133";
$ip = $REMOTE _addr;
$WRU-&GT;WRU ($ip, $szResult);
echo $ip. " <br> ";
Echo $szResult;
//---------------------------------------------------------------------------
?>



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.