php查询ip所在地代码
/**
*@ date 2010.12.21
*@ author 王刚
*@ email 373882774@qq.com
*@ qq 373882774
注:文件头 [第一条索引的偏移量 (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("221.231.102.100");
//$address=$ip->getaddress(www.111cn.net);
echo '';<br> print_r($address);<br> ?>Copy after login

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible
