执行程序:
<?
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 "
// 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 "