首页 后端开发 php教程 php查询ip所在地的方法_php技巧

php查询ip所在地的方法_php技巧

May 16, 2016 pm 08:28 PM
ip php 查询

本文实例讲述了php查询ip所在地的方法。分享给大家供大家参考。

具体实现方法如下:

复制代码 代码如下:
/**
*@ date         2010.12.21
注:文件头 [第一条索引的偏移量 (4byte)] + [最后一条索引的偏移地址 (4byte)]     8字节
记录区 [结束ip (4byte)] + [地区1] + [地区2]                                4字节+不定长
索引区 [开始ip (4byte)] + [指向记录区的偏移地址 (3byte)]                   7字节
*/
class iplocation{
var $fp;
var $firstip;  //第一条ip索引的偏移地址
var $lastip;   //最后一条ip索引的偏移地址
var $totalip;  //总ip数
/*
|----------------------------------------------------------------------------
| 构造函数,初始化一些变量
|----------------------------------------------------------------------------
|
*/
function iplocation($datfile = "qqwry.dat"){
$this->fp=fopen($datfile,'rb')or die("qqwry.dat不存在,请去网上 下载纯真ip数据 库, 'qqwry.dat' 放到当前目录下");   //二制方式打开
$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)));
}
/*
|----------------------------------------------------------------------------
| 获取地址信息
|----------------------------------------------------------------------------
|
*/
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 $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 $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"]=" ";
}
return $address;
}
}
 
/*用法如下:*/
$ip=new iplocation("qqwry.dat");
$address=$ip->getaddress("61.129.51.27");
//$address=$ip->getaddress(www.jb51.net);
echo '
'; <br>
print_r($address); <br>
?>
登录后复制

希望本文所述对大家的PHP程序设计有所帮助。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

See all articles