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. Android教程2020 - RecyclerView响应点击

    本文介绍RecyclerView设置点击的方法.这里给出比较常见的使用方式. Android教程2020 - 系列总览 本文链接 前面我们已经知道如何用RecyclerView显示一列数据. 用户点击 ...

  2. Centos 7 最小化部署svn版本控制(http协议)

    1.关闭selinux sh-4.2# sed -i 's/enforcing/disabled/' /etc/selinux/config sh-4.2# reboot 2.卸载防火墙 sh-4.2 ...

  3. java 排序算法分析

    一.冒泡排序(时间复杂度O(N^2)) public int[] bubbling(int[] arr){ ) return arr; ; i--){ 1 ; j < i-; j ++){ 2 ...

  4. linux 移动复制删除

    linux下文件的复制.移动与删除命令为:cp,mv,rm一.文件复制命令cp    命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination)        ...

  5. Redis | 使用redis存储对象反序列化异常SerializationFailedException

    案例 使用Redis进行对象存储,在处理业务逻辑的时候,丛Redis获取对象发现反序列化失败,抛出如下异常: Caused by: org.springframework.data.redis.ser ...

  6. 20190708三人开黑CF模拟赛

    7月8号晚上8点和两位巨佬开了一场虚拟cf: [Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)] 我这么蔡,只A ...

  7. JsonCpp中文unicode编码问题

    用JsonCpp解析C++中文unicode编码,转换问题,今天找打了一种解决办法 这个问题画了不少时间,贴出来供大家参考 在json_reader.cpp中找到codePointToUTF8函数,添 ...

  8. golang学习笔记(一):包,变量,函数

    欢迎访问我的博客和github! go 语言学习笔记第一弹,来自 gotour ,以后要常写笔记,把自己学习笔记记录下来,就算只是笔记也要多写. 好记性不如烂笔头,也要多锻炼自己的写作能力. 说实话, ...

  9. shell命令之一天一见:grep

    一. 简介 grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来 ...

  10. Codeforces_812

    A. 每条人行道有六条车道会撞到. #include<bits/stdc++.h> using namespace std; ],b[],c[],d[]; int main() { ios ...