get_add();
//验证ip
function match_ip($ip){ //0.0.0.0 -- 255.255.255.255
$pattern = "/^(((1?\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((1?\d{1,2})|(2[0-4]\d)|(25[0-5]))$/";
return preg_match($pattern,$ip);
}
//连续空格合成一个
function merge_spaces($string){
return preg_replace("/\s(?=\s)/","\\1",$string);
}
function get_add(){
if(isset($_POST['ip'])){
$ip=$_POST['ip'];
if(!match_ip(str_replace(" ","",$ip))){
echo "
请输入有效的IP地址
";}else{
$file = fopen("ip.txt", "r") or die("Unable to open file!");
//按行读取文件
while(!feof($file))
{
$str= fgets($file);
$str=merge_spaces($str);
$s=explode(' ',$str);
if(ip2long($ip)>=ip2long($s[0])&&ip2long($ip)<=ip2long($s[1])){
$add="";
for($i=2;$i
if($i!=count($s)-1){
$add=$add.$s[$i]." ";
}else{
$add=$add.$s[$i];
}
}
echo $add;
break;
}
}
fclose($file);
}
}
}
?>