很多站长朋友们都不太清楚datip库php,今天小编就来给大家整理datip库php,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php用IP查询归属地 2、 关于php的文件dat 3、 php网站 我有ip.dat数据库,如何调取ip.dat并将访客的IP地址、归属地和时间写入.tx php用IP查询归属地class ip_location
{
function init()
{
$this->wrydat = 'ip_area.dat';
$this->fp = fopen($this->wrydat, 'rb');
$this->getipnumber();
$this->getwryversion();
$this -> REDIRECT_MODE_0 = 0;
$this -> REDIRECT_MODE_1 = 1;
$this -> REDIRECT_MODE_2 = 2;
}
function get($str)
{
return $this->$str;
}
function set($str,$val)
{
$this->$str = $val;
}
function getbyte($length,$offset=null)
{
!is_null($offset) fseek($this->fp, $offset, SEEK_SET);
return fread($this->fp, $length);
}
function packip($ip)
{
return pack('N', intval(ip2long($ip)));
}
function getlong($length=4, $offset=null)
{
$chr=null;
for($c=0;$length%4!=0$c<(4-$length%4);$c++)
{
$chr .= chr(0);
}
$var = unpack( 'Vlong', $this->getbyte($length, $offset).$chr);
return $var['long'];
}
function getwryversion()
{
$length = preg_match("/coral/i",$this->wrydat)?26:30;
$this->wrydat_version = $this->getbyte($length, $this->firstip-$length);
}
function getipnumber()
{
$this->firstip = $this->getlong();
$this->lastip = $this->getlong();
$this->ipnumber = ($this->lastip-$this->firstip)/7+1;
}
function getstring($data='', $offset=NULL)
{
$char = $this->getbyte(1,$offset);
while(ord($char) > 0)
{
$data .= $char;
$char = $this->getbyte(1);
}
return $data;
}
function iplocaltion($ip)
{
$ip = $this->packip($ip);
$low = 0;
$high = $this->ipnumber-1;
$ipposition = $this->lastip;
while($low <= $high)
{
$t = floor(($low+$high)/2);
if($ip < strrev($this->getbyte(4,$this->firstip+$t*7)))
$high = $t - 1;
else
{
if($ip > strrev($this->getbyte(4,$this->getlong(3))))
$low = $t + 1;
else
{
$ipposition = $this->firstip+$t*7;
break;
}
}
}
return $ipposition;
}
function getarea()
{
$b = $this->getbyte(1);
switch(ord($b))
{
case $this -> REDIRECT_MODE_0 :
return '';
break;
case $this -> REDIRECT_MODE_1:
case $this -> REDIRECT_MODE_2:
return $this->getstring('',$this->getlong(3));
break;
default:
return $this->getstring($b);
break;
}
}
function getiplocation($ip)
{
$ippos = $this->iplocaltion($ip);
$this->ip_range_begin = long2ip($this->getlong(4,$ippos));
$this->ip_range_end = long2ip($this->getlong(4,$this->getlong(3)));
$b = $this->getbyte(1);
switch(ord($b))
{
case $this -> REDIRECT_MODE_1:
$b = $this->getbyte(1,$this->getlong(3));
if(ord($b) == $this -> REDIRECT_MODE_2)
{
$countryoffset = $this->getlong(3);
$this->area = $this->getarea();
$this->country = $this->getstring('',$countryoffset);
}
else
{
$this->country = $this->getstring($b);
$this->area = $this->getarea();
}
break;
case $this -> REDIRECT_MODE_2:
$countryoffset = $this->getlong(3);
$this->area = $this->getarea();
$this->country = $this->getstring('',$countryoffset);
break;
default:
$this->country = $this->getstring($b);
$this->area = $this->getarea();
break;
}
}
}
---------------------------------------------------------------
调用方法:
$iploca = new ip_location;
$iploca -> init();
$iploca -> getiplocation($ip);
$area['country'] = str_replace(array('CZ88.NET'), '', $iploca -> get('country'));
$area['area'] = str_replace(array('CZ88.NET'), '', $iploca -> get('area'));
$area['country']=='' $area['country']='未知';
$area['area']=='' $area['area']='未知';
return $area;
关于php的文件dat这个是自己构造的一个存储文件,要看源码了解存储格式后,才能按照源码规定的格式写入数据。源码不长的话放出来看看。
补充:
搜了下你说的数据库,如果你说的数据库是名人名言数据库,其数据结构很简单。每条记录包括人名和名言,以行为单位存放,人名和名言之间用制表符隔开。
写入时可以这样:
$name="人名";
$note="名言";
$fp=fopen('myan.dat','a');
fwrite($fp,"$name\t$note\r\n");
fclose($fp);
php网站 我有ip.dat数据库,如何调取ip.dat并将访客的IP地址、归属地和时间写入.txPHP可以获取到访客的IP,然后查询归属地,这个网上很多接口有提供
关于datip库php的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。