PHP做的IP归属地查询程序,文本数据库

执行程序:
<? 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 "
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值