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 ...
随机推荐
- Mybatis基础(二)
Mybatis连接池 Mybatis连接池提供了三种配置方式,配置的位置在SqlMapConfig.xml的dataSource标签中,其type属性就是配置连接池的种类.type的可取值 1.POO ...
- <背包>solution_CF366C_Dima and Salad
Dima and Salad Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. T ...
- vue中如何在本地导入js文件
import {setStore,setUser,getStore,removeStore} from "../../../public/localstory" 在导入js文件时, ...
- 记一次golang的内存泄露
程序功能 此程序的主要功能是将文件中数据导入到clickhouse数据库中. [问题描述] 服务器内存每隔一段时间会耗尽 [问题分析] 由于使用的是go语言开发的,所以采用了业界流行的工具pprof. ...
- Docker 代理脱坑指南
Docker 代理配置 由于公司 Lab 服务器无法正常访问公网,想要下载一些外部依赖包需要配置公司的内部代理.Docker 也是同理,想要访问公网需要配置一定的代理. Docker 代理分为两种,一 ...
- python UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 87: illegal multibyte sequence异常解决
我们处理文本文件时,经常会遇到这样的报错: UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 87: illegal ...
- 国外大神制作的一个很棒的matplotlib 可视化教程
国外大神制作的一个很棒的matplotlib 可视化教程 参考:https://www.machinelearningplus.com/plots/top-50-matplotlib-visualiz ...
- WeChall_Prime Factory (Training, Math)Training: WWW-Robots (HTTP, Training)
In this little training challenge, you are going to learn about the Robots_exclusion_standard.The ro ...
- centos7 上为php-fpm安装gd扩展库
转自:https://blog.csdn.net/liyyzz33/article/details/89166110 首先查看自己当前php的版本 php -v PHP 5.6.40 查看yum中是否 ...
- ELF文件之四——使用链接脚本-2个函数-data
main.c ; int main() { ; } int add() { ; } main.o 反汇编可以看到多了.text节的反汇编,存储的是全局变量的初始化数值 main.o对比,text段后面 ...