php读取qqwry.dat ip地址定位文件的类
<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
class iplocate{
var $fp;
var $firstip; //第一条ip索引的偏移地址
var $lastip; //最后一条ip索引的偏移地址
var $totalip; //总ip数
// 获取客户端IP地址
function get_client_ip(){
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($ip);
}
//*
//构造函数,初始化一些变量
//$datfile 的值为纯真IP数据库的名子,可自行修改.
//*
function iplocate(){
$datfile = "ip1.dat";
$this->fp=fopen($datfile,'rb'); //二制方式打开
$this->firstip = $this->get4b(); //第一条ip索引的绝对偏移地址
$this->lastip = $this->get4b(); //最后一条ip索引的绝对偏移地址
$this->totalip =($this->lastip - $this->firstip)/7 ; //ip总数 索引区是定长的7个字节,在此要除以7,
register_shutdown_function(array($this,"closefp")); //为了兼容php5以下版本,本类没有用析构函数,自动关闭ip库.
}
//*
//关闭ip库
//*
function closefp(){
fclose($this->fp);
}
//*
//读取4个字节并将解压成long的长模式
//*
function get4b(){
$str=@unpack("V",fread($this->fp,4));
return $str[1];
}
//*
//读取重定向了的偏移地址
//*
function getoffset(){
$str=@unpack("V",fread($this->fp,3).chr(0));
return $str[1];
}
//*
//读取ip的详细地址信息
//*
function getstr(){
$split=fread($this->fp,1);
while (ord($split)!=0) {
$str .=$split;
$split=fread($this->fp,1);
}
return $str;
}
//*
//将ip通过ip2long转成ipv4的互联网地址,再将他压缩成big-endian字节序
//用来和索引区内的ip地址做比较
//*
function iptoint($ip){
return pack("N",intval(ip2long($ip)));
}
//*
//获取客户端ip地址
//注意:如果你想要把ip记录到服务器上,请在写库时先检查一下ip的数据是否安全.
//*
function getIP() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) { //获取客户端用代理服务器访问时的真实ip 地址
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
//*
//获取地址信息
//*
function readaddress(){
$now_offset=ftell($this->fp); //得到当前的指针位址
$flag=$this->getflag();
switch (ord($flag)){
case 0:
$address="";
break;
case 1:
case 2:
fseek($this->fp,$this->getoffset());
$address=$this->getstr();
break;
default:
fseek($this->fp,$now_offset);
$address=$this->getstr();
break;
}
return $address;
}
//*
//获取标志1或2
//用来确定地址是否重定向了.
//*
function getflag(){
return fread($this->fp,1);
}
//*
//用二分查找法在索引区内搜索ip
//*
function searchip($ip){
$ip=gethostbyname($ip); //将域名转成ip
$ip_offset["ip"]=$ip;
$ip=$this->iptoint($ip); //将ip转换成长整型
$firstip=0; //搜索的上边界
$lastip=$this->totalip; //搜索的下边界
$ipoffset=$this->lastip; //初始化为最后一条ip地址的偏移地址
while ($firstip <= $lastip){
$i=floor(($firstip + $lastip) / 2); //计算近似中间记录 floor函数记算给定浮点数小的最大整数,说白了就是四舍五也舍
fseek($this->fp,$this->firstip + $i * 7); //定位指针到中间记录
$startip=strrev(fread($this->fp,4)); //读取当前索引区内的开始ip地址,并将其little-endian的字节序转换成big-endian的字节序
if ($ip < $startip) {
$lastip=$i - 1;
}
else {
fseek($this->fp,$this->getoffset());
$endip=strrev(fread($this->fp,4));
if ($ip > $endip){
$firstip=$i + 1;
}
else {
$ip_offset["offset"]=$this->firstip + $i * 7;
break;
}
}
}
return $ip_offset;
}
//*
//获取ip地址详细信息
//*
function getaddress($ip){
$ip_offset=$this->searchip($ip); //获取ip 在索引区内的绝对编移地址
$ipoffset=$ip_offset["offset"];
$address["ip"]=$ip_offset["ip"];
fseek($this->fp,$ipoffset); //定位到索引区
$address["startip"]=long2ip($this->get4b()); //索引区内的开始ip 地址
$address_offset=$this->getoffset(); //获取索引区内ip在ip记录区内的偏移地址
fseek($this->fp,$address_offset); //定位到记录区内
$address["endip"]=long2ip($this->get4b()); //记录区内的结束ip 地址
$flag=$this->getflag(); //读取标志字节
switch (ord($flag)) {
case 1: //地区1地区2都重定向
$address_offset=$this->getoffset(); //读取重定向地址
fseek($this->fp,$address_offset); //定位指针到重定向的地址
$flag=$this->getflag(); //读取标志字节
switch (ord($flag)) {
case 2: //地区1又一次重定向,
fseek($this->fp,$this->getoffset());
$address["area1"]=$this->getstr();
fseek($this->fp,$address_offset+4); //跳4个字节
$address["area2"]=$this->readaddress(); //地区2有可能重定向,有可能没有
break;
default: //地区1,地区2都没有重定向
fseek($this->fp,$address_offset); //定位指针到重定向的地址
$address["area1"]=$this->getstr();
$address["area2"]=$this->readaddress();
break;
}
break;
case 2: //地区1重定向 地区2没有重定向
$address1_offset=$this->getoffset(); //读取重定向地址
fseek($this->fp,$address1_offset);
$address["area1"]=$this->getstr();
fseek($this->fp,$address_offset+8);
$address["area2"]=$this->readaddress();
break;
default: //地区1地区2都没有重定向
fseek($this->fp,$address_offset+4);
$address["area1"]=$this->getstr();
$address["area2"]=$this->readaddress();
break;
}
//*过滤一些无用数据
if (strpos($address["area1"],"CZ88.NET")!=false){
$address["area1"]="未知";
}
if (strpos($address["area2"],"CZ88.NET")!=false){
$address["area2"]=" ";
}
foreach($address as $k=>$item)
{
if(!$this->is_utf8($address[$k]))
{
$address[$k] = iconv('gbk','utf-8',$item);
}
}
return $address;
}
function is_utf8($string)
{
return preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string);
}
}
?>
项目地址:https://github.com/Wen1750686723/iplocate.git
php读取qqwry.dat ip地址定位文件的类的更多相关文章
- thinkphp的ip地址定位
在WEB应用中,根据IP地址定位和记录相关访问日志也是非常常见的需求,在ThinkPHP中你可以轻松的实现IP地址获取和定位. 获取扩展类库 可以在官网的http://www.thinkphp.cn/ ...
- ip地址定位库
ip2region 1.2.1 发布了,新增 Python 内存查询+数据文件更新. 准确率99.9%的ip地址定位库,0.0x毫秒级查询,数据库文件大小只有1.5M,提供了java, php, c, ...
- 准确率99.9%的离线IP地址定位库
Ip2region是什么? ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nod ...
- 获取客户端IP地址定位城市信息
获取客户端IP地址定位城市信息 1.首先获取客户端的IP地址 function getIPaddress(){ $IPaddress=''; if (isset($_SERVER)){ if (iss ...
- QT通过IP地址定位地址(用get方法取数据)
通过IP地址定位地址,是要通过查询数据库,如果自己做一个这样的数据库工作量就比较大,所以在网上找了一个查询IP地址的网址,通过调用这个网址查询来实现,但是这个有一定的弊端,如果没有网络或者这个网址不可 ...
- 通过IP地址定位准确的地理位置
事情的经过时这样的: 朋友发来一封QQ邮件原文,询问里面显示的IP地址是不是真是的IP地址.然后,我就解锁了一项新技能:通过IP地址定位准确的地理位置 在这里收藏一下这个网址:http://www.8 ...
- IP地址 A\B\C类
互联网协议地址(英语:Internet Protocol Address,又译为网际协议地址),缩写为IP地址(IP Address),在Internet上,一种给主机编址的方式.常见的IP地址,分为 ...
- IP地址分类(A类 B类 C类 D类 E类)
IP地址分类(A类 B类 C类 D类 E类) IP地址由四段组成,每个字段是一个字节,8位,最大值是255,, IP地址由两部分组成,即网络地址和主机地址.网络地址表示其属于互联网的哪一个网络,主机地 ...
- 根据外网ip地址定位用户所在城市
package com.henu.controller; import java.io.BufferedReader; import java.io.DataOutputStream; import ...
随机推荐
- loadrunner中定义数组
loadrunner 中数组的定义 loadrunner 中数组的定义: lr_save_string("www.google.com","website_1" ...
- SU Demos-02Filtering-03Sudipfilt
不足之处,欢迎各位博友批评指正. 进入目录,依照惯例先看Readme, 第一个脚本, 下面是运行结果: 第二个脚本: 运行结果如下: 第三个脚本: 运行结果:
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
- LightOJ1031 Easy Game(区间DP)
我可能真想不到这题是区间DP,不过知道是区间DP想了下就AC了. dp[i][j]表示局面为ai...aj先手能获得与后手得分的最大差值 那么转移到当前状态就是枚举中间的位置,分成两边,其中一边先手全 ...
- python 代码片段15
#coding=utf-8 ''' 如果运行时发生异常的话,解释器会查找相应的处理函数.要是在当前函数里没有 找到的话,它会将异常传递给上层的调用函数,看看那里能不能处理.如果在在最 外层还没有找到的 ...
- !cocos2d 重复添加action事件
当点击的时候,如果不是按照开始点击计算的,那么持续点击会导致不会变大. void Piece::setActived(bool active) { _actived = active; CCActio ...
- Robotium 测试方法
1.检查CheckBox 是否选上,用solo.isCheckBoxChecked( “text” ). 有时候checkBox 没有相关的text,这时要用solo.isCheckBoxChecke ...
- Google Code Jam 2009 Qualification Round Problem C. Welcome to Code Jam
本题的 Large dataset 本人尚未解决. https://code.google.com/codejam/contest/90101/dashboard#s=p2 Problem So yo ...
- ibatis插入数据返回ID的方法
ibatis插入数据返回ID的方法 主要就是利用seelctkey来获取这个ID值,但是oracle和mysql的区别还是很大的 oracle的用法 <insert id="inser ...
- 网页制作常见的问题(怎样兼容IE6/IE7/火狐浏览器)
1.IE6双边距问题? 在IE6的浏览器中明明设置的是10px的margin却为什么显示的是20px的margin其实这个Ie6的一个双边距BUG 例如: <style type="t ...