执行程序:
<?
PHP
//
IP数据所在的目录
$ipdata
=
"
ipdata
"
;

/**/
/*
功能:IP归属地查询
*/
//
-------------------
function Ip2Area($ipdata,$cur_ip)

{
$sect=explode(".",$cur_ip);
for ($i=0; $i<4; $i++)

{
$sect[$i]=sprintf("%03d", $sect[$i]);
}
//-------------------------
//寻找相应的文件
$handle = opendir($ipdata);
while($file =readdir($handle))

{
if($file=="."||$file=="..")continue;
$tar_1 = substr($file,0,3);
$tar_2 = substr($file,4,3);
$tar_3 = substr($file,8,3);
$tar_4 = substr($file,12,3);
if($sect[0]>=$tar_1&&$sect[0]<=$tar_3&&$sect[1]>=$tar_2&&$sect[1]<=$tar_4)

{
$fname = $file;
break;
}
}
$ipfile = file($ipdata."/".$fname);
$tempip=implode(".",$sect);
for($i=0;$i<sizeof($ipfile);$i++)

{
$data_ip = explode("__",$ipfile[$i]);
if(strcmp($tempip,$data_ip[0])>=0&&strcmp($tempip,$data_ip[1])<=0)

{
$area = trim($data_ip[2])." ".trim($data_ip[3]);
break;
}
else $area = "未知地区";
}
return $area;
}
if
($_GET[
'
ip
'
]
!=
""
)

{
$arte = "查询结果:".Ip2Area($ipdata,$_GET['ip']);
}
?>
<
meta http
-
equiv
=
"
Content-Type
"
content
=
"
text/html; charset=gb2312
"
>
<
title
>
IP归属地查询
</
title
>
<
table width
=
"
350
"
height
=
"
27
"
border
=
"
0
"
align
=
"
center
"
cellpadding
=
"
0
"
cellspacing
=
"
1
"
style
=
"
font-size:9pt; color:#FFFFFF;
"
>
<
tr
>
<
td height
=
"
35
"
><
font color
=
"
#000000
"
>
您的IP:
<
font color
=
"
#FF0000
"
><?=
$_SERVER[
'
REMOTE_ADDR
'
];
?></
font
>&
nbsp;位置:
<?=
Ip2Area($ipdata,$_SERVER[
'
REMOTE_ADDR
'
]);
?></
font
></
td
>
</
tr
>
<
form name
=
"
form1
"
method
=
"
get
"
action
=
"
index.php
"
><
tr
>
<
td height
=
"
35
"
align
=
"
center
"
bgcolor
=
"
#666666
"
>
请输入IP地址:
<
input name
=
"
ip
"
type
=
"
text
"
id
=
"
ip
"
size
=
"
15
"
style
=
"
height: 20px; border: 1px solid #FFFFFF; background-color:#CCCCCC;
"
>
<
input type
=
"
submit
"
name
=
"
Submit
"
value
=
"
查询
"
style
=
"
height: 20px; border: 1px solid #FFFFFF; background-color:#CCCCCC;
"
>
</
td
>
</
tr
></
form
>
PHP做的IP归属地查询程序,文本数据库
最新推荐文章于 2025-11-24 09:43:36 发布
这是一个使用PHP编写的IP归属地查询程序,通过读取IP数据文件并进行格式化处理,实现快速查找IP对应的位置。程序首先将IP地址转换为特定格式,然后根据IP段寻找对应的文件,并在文件中查找匹配的IP范围,返回地区信息。同时提供了将大文件拆分成多个小文件的功能,提高检索效率。

最低0.47元/天 解锁文章
972

被折叠的 条评论
为什么被折叠?



