http://www.cz88.net/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转换省市县的更多相关文章

  1. 如何把IP转换成经纬度(Java版)

    经常有这种需求,拥有用户的IP地址,想要在地图上显示用户的访问量.这个时候就需要用到经纬度...应为一般的地图插件都是基于经纬度的. 那么问题来了,如何把IP转换成经纬度? 百度API 最国产的方式, ...

  2. mysql IP转换函数

    1.将字符串类型的实际IP转换成十进制数值型的 SELECT INET_ATON('209.207.224.40'); 执行结果:

  3. IP转换成域名

    DNS就是域名解析系统,它可以将IP转换成域名,也可以将域名转换成IP 1. 安装DNS服务 开始—〉设置—〉控制面板—〉添加/删除程序—〉添加/删除Windows组件—〉“网络服务”—〉选择“域名服 ...

  4. Mysql时间戳函数和ip转换函数

    Mysql中对于unix时间戳的转换还是挺方便的, 1.转换为时间戳 select unix_timestamp('2013-07-15 10-06-07') 如果参数为空,则为当前时间 2.转换为时 ...

  5. php的IP转换成整型函数ip2long()易出现负数

    php中将IP转换成整型的函数ip2long()容易出现问题,在IP比较大的情况下,会变成负数.如下<?php$ip = "192.168.1.2";$ip_n = ip2l ...

  6. IP转换成LONG 的 问题

    如何将四个字段以点分开的IP网络址协议地址转换成整数呢?PHP里有这么一个函数ip2long.比如 <?php echo ip2long("10.2.1.3"); ?> ...

  7. php IP转换整形(ip2long)

    如何将四个字段以点分开的IP网络址协议地址转换成整数呢?PHP里有这么一个函数ip2long.比如 <?php echo ip2long("10.2.1.3"); ?> ...

  8. 数字、ip转换python实现

    # 数字 ==> ip # 数字范围[0, 255^4] >>> num2ip = lambda x: '.'.join([str(x/(256**i)%256) for i ...

  9. IP转换hash以及返回

    InetAddress address = InetAddress.getByName("127.0.0.1"); System.out.println(address); int ...

随机推荐

  1. HTML 中清除浮动

    html中如何清除浮动   在html中,浮动可以说是比较常用的.在页面的布局中他有着很大的作用,但是浮动中存在的问题也是比较多的.现在我们简单说一下怎么去除浮动 首先我们先简单的看一下浮动: 首先我 ...

  2. Python变量、方法、类的命名规则

    1. 变量命名总结: - 1.单下划线开头变量:protected - 2.双下划线开头变量:private - 3.双下划线开头,双下划线结尾:系统内置变量 2. 函数命名总结: - 1.私有方法: ...

  3. C# list中ConvertAll的使用

    static double TakeSquareRoot(int x) { //return Math.Sqrt(x); ; } static void Main(string[] args) { L ...

  4. CCF_ 201312-3_最大的矩形

    遍历数组中每一元素,左右延伸得出宽度. #include<iostream> #include<cstdio> using namespace std; int main() ...

  5. BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线 (二分+最短路)

    题意: 给一个2e4带正边权的图,可以免费k个边,一条路径的花费为路径上边权最大值,问你1到n的最小花费 思路: 对于一个x,我们如果将大于等于x的边权全部免费,那么至少需要免费的边的数量就是 “设大 ...

  6. 将Spring实战第5版中Spring HATEOAS部分代码迁移到Spring HATEOAS 1.0

    最近在阅读Spring实战第五版中文版,书中第6章关于Spring HATEOAS部分代码使用的是Spring HATEOAS 0.25的版本,而最新的Spring HATEOAS 1.0对旧版的AP ...

  7. Sklearn--(SVR)Regression学习笔记

    今天介绍一个机器学习包,sklearn.其功能模块有regression\classification\clustering\Dimensionality reduction\data preproc ...

  8. [redis读书笔记] 第一部分 数据结构与对象 压缩列表

    压缩列表是为了节省内存而设计的,是列表键和哈希键的底层实现之一. 压缩列表的逻辑如下,

  9. TomCat控制台中文乱码及IDEA设置为UTF-8

    一.解决IDEA中的中文乱码 1.首先设置idea编辑器的编码: File-Setting设置如下 idea显示编码:windows默认用gbk所以idea显示默认为gbk编码,在 Help--Edi ...

  10. linux 删除文件 磁盘空间未释放

    具体情况就是:删除了一个超大文件后,发现磁盘空间没有变化 原因:有进程正在使用这个文件,虽然我们从文件系统的目录结构上解除链接(unlink),然而文件是被 打开的(有一个进程正在使用),那么进程将仍 ...