get查询IP PHP源码,IP来源查询php源代码_PHP

P>《追捕》现在已经响当当了,他的成功,在于它的数据库的维护的毅力,

很多软件都在用它的数据,有些改了名字,变成xxx.dat了,嗬嗬,我觉得不太可取,有点儿不地道的感觉。

我的代码也用到了他,改不改名字无所谓了,关键尊重人家的劳动就好了。

在网页上增加一个段简单的代码,就可以显示来访者的地点。

还可以在自己的网站里面写一个个性化的日志,记录ip也记录地点。

效果可以查看 www.fogsun.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的自然数。

* ********************************************************************/

?>

// Test Code.

$wru= new TWru;

$szResult="";

$ip= "202.96.134.133";

// $ip= $REMOTE_ADDR;

$wru->wru($ip, $szResult);

echo $ip."

";

echo $szResult;

//---------------------------------------------------------------------------

?>

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值