php ip转换省市县
http://www.ttlsa.com/php/php_cunzhen-ipdata/
# wget h http://6.scdx3.crsky.com/201307/qqwry0715.zip
# unzip qqwry0715.zip
# mv ip/qqwry.dat /data/site/test.ttlsa.com/qqwry.dat
p
class cls_ipAddress{
private $fp;
private $firstip;
private $lastip;
private $totalip;
public function __construct($filename="qqwry.dat"){
$this->fp=0;
if(($this->fp=@fopen($filename,"rb"))!==false){
$this->firstip=$this->getlong();
$this->lastip=$this->getlong();
$this->totalip=($this->lastip-$this->firstip)/7;
register_shutdown_function(array(&$this,"__destruct"));
}
}
public function __destruct(){
if($this->fp){
@fclose($this->fp);
}
$this->fp=0;
}
private function getlong(){
$result=unpack("Vlong",fread($this->fp,4));
return $result["long"];
}
private function getlong3(){
$result=unpack("Vlong",fread($this->fp,3).chr(0));
return $result["long"];
}
private function packip($ip){
return pack("N",intval(ip2long($ip)));
}
private function getstring($data=""){
$char=fread($this->fp,1);
while(ord($char)>0){
$data.=$char;
$char=fread($this->fp,1);
}
return $data;
}
private function getarea(){
$byte=fread($this->fp,1);
switch(ord($byte)){
case 0:
$operators="";
break;
case 1:
case 2:
fseek($this->fp,$this->getlong3());
$operators=$this->getstring();
break;
default:
$operators=$this->getstring($byte);
break;}
return $operators;
}
public function getlocation($ip){
if(!$this->fp){return null;}
$location["ip"]=gethostbyname($ip);
$ip=$this->packip($location["ip"]);
$l=0;
$u=$this->totalip;
$findip=$this->lastip;
while($l<=$u){
$i=floor(($l+$u)/2);
fseek($this->fp,$this->firstip+$i*7);
$startip=strrev(fread($this->fp,4));
if($ip<$startip){
$u=$i-1;
}else{
fseek($this->fp,$this->getlong3());
$endip=strrev(fread($this->fp,4));
if($ip>$endip){
$l=$i+1;
}else{
$findip=$this->firstip+$i*7;
break;
}
}
}
fseek($this->fp,$findip);
$location["startip"]=long2ip($this->getlong());
$offset=$this->getlong3();
fseek($this->fp,$offset);
$location["endip"]=long2ip($this->getlong());
$byte=fread($this->fp,1);
switch(ord($byte)){
case 1:
$countryOffset=$this->getlong3();
fseek($this->fp,$countryOffset);
$byte=fread($this->fp,1);
switch(ord($byte)){
case 2:
fseek($this->fp,$this->getlong3());
$location["area"]=$this->getstring();
fseek($this->fp,$countryOffset+4);
$location["operators"]=$this->getarea();
break;
default:
$location["area"]=$this->getstring($byte);
$location["operators"]=$this->getarea();
break;}
break;
case 2:
fseek($this->fp,$this->getlong3());
$location["area"]=$this->getstring();
fseek($this->fp,$offset+8);
$location["operators"]=$this->getarea();
break;
default:
$location["area"]=$this->getstring($byte);
$location["operators"]=$this->getarea();
break;}
if($location["area"]=="CZ88.NET"){
$location["area"]="未知";
}
if($location["operators"]=="CZ88.NET"){
$location["operators"]="未知";
}
return $location;
}
}
php ip转换省市县的更多相关文章
- 如何把IP转换成经纬度(Java版)
经常有这种需求,拥有用户的IP地址,想要在地图上显示用户的访问量.这个时候就需要用到经纬度...应为一般的地图插件都是基于经纬度的. 那么问题来了,如何把IP转换成经纬度? 百度API 最国产的方式, ...
- mysql IP转换函数
1.将字符串类型的实际IP转换成十进制数值型的 SELECT INET_ATON('209.207.224.40'); 执行结果:
- IP转换成域名
DNS就是域名解析系统,它可以将IP转换成域名,也可以将域名转换成IP 1. 安装DNS服务 开始—〉设置—〉控制面板—〉添加/删除程序—〉添加/删除Windows组件—〉“网络服务”—〉选择“域名服 ...
- Mysql时间戳函数和ip转换函数
Mysql中对于unix时间戳的转换还是挺方便的, 1.转换为时间戳 select unix_timestamp('2013-07-15 10-06-07') 如果参数为空,则为当前时间 2.转换为时 ...
- php的IP转换成整型函数ip2long()易出现负数
php中将IP转换成整型的函数ip2long()容易出现问题,在IP比较大的情况下,会变成负数.如下<?php$ip = "192.168.1.2";$ip_n = ip2l ...
- IP转换成LONG 的 问题
如何将四个字段以点分开的IP网络址协议地址转换成整数呢?PHP里有这么一个函数ip2long.比如 <?php echo ip2long("10.2.1.3"); ?> ...
- php IP转换整形(ip2long)
如何将四个字段以点分开的IP网络址协议地址转换成整数呢?PHP里有这么一个函数ip2long.比如 <?php echo ip2long("10.2.1.3"); ?> ...
- 数字、ip转换python实现
# 数字 ==> ip # 数字范围[0, 255^4] >>> num2ip = lambda x: '.'.join([str(x/(256**i)%256) for i ...
- IP转换hash以及返回
InetAddress address = InetAddress.getByName("127.0.0.1"); System.out.println(address); int ...
随机推荐
- alert弹出窗口,点击确认后关闭页面
alert("点击确认后,关闭页面"); window.opener=null;window.top.open('','_self','');window.close(this);
- Fedora 安装及配置
引言 最近学习课程要用到Linux,之前装的Ubuntu双系统被我删掉了(因为后来发现那个WSL,win子系统还挺好用的),所以上午用虚拟机再装了一下老师给的Ubuntu16,也不知道怎么回事特别卡, ...
- ubuntu以root身份登录
- 实验8:路由器IOS升级2
IOS 升级 在介绍CISCO路由器IOS升级方法前,有必要对Cisco路由器的存储器的相关知识作以简单介绍.路由器与计算机相似,它也有内存和操作系统.在Cisco路由器中,其操作系统叫做互连网操作系 ...
- LUA学习笔记(第18-20章)
数学库 print(math.pi)-->π print(math.huge)-->Lua中表示的最大数字 --[[ 3.1415926535898 1.#INF ]] print(mat ...
- POJ_1166_暴搜
题目描述: 有3*3的9个时钟,每个始终有0,1,2,3四种可以循环的状态码,每组数据给我们9个时钟的一种状态码.另外还有9种操作,分别使指定位置的时钟状态码加一,求使得9个时钟状态码全部置于0的最少 ...
- 【OpenGL】LNK1104 无法打开文件“freeglutd.lib”
新建的OpenGL程序编译时经常会出现[LNK1104 无法打开文件“freeglutd.lib”]问题,如果freeglutd.lib确实放到了正确的路径下,通常可以通过添加“NDEBUG”宏定义解 ...
- 安装symfony3.4的坑,也是PHP7.3的经典坑之解决办法
对于刚入手symfony3.4的同学,肯定会发现,安装symfony后部署后看到的往往不是hello world,也不是symfony的欢迎页面,而是给你一个下马威,唉,给你来个bug开开胃. 当然这 ...
- Method Resolve Order (MRO) - 类对象属性的解析顺序
Method Resolve Order (MRO) - 类对象属性的解析顺序 Python 支持多重继承, 此时就需要解决按照何种顺序来解析属性的问题.类的继承关系在一个特殊的类属性中指定(__mr ...
- SpringProfile轻松切换多环境配置文件
在项目开发的过程中,我们难免会遇到开发.测试.生产等环境的切换,而各个环境的配置肯定是不同的.传统的办法是在项目打包的时候修改配置文件.但人为做的事情难免产生意外.Spring 为我们提供了一种多环境 ...