简体   繁体   English

PHP验证IP地址

[英]PHP validate IP address

<?php
$subdomain ='hello.cheapantivirus.me';
        $ns1 = 'ns01.000webhost.com';

$host = "@$ns1 $subdomain";
$ip = `/usr/bin/dig $host +short A`;

 echo $ip; // output is 31.170.161.67

 $ip3 = '84.8.161.5';

 if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
   echo "valid"; 
}
else {
  echo " not valid"; // the $ip is invalid
}


 if(filter_var($ip3, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
   echo "valid"; // somehow this one is valid
}
else {
  echo " not valid";
}
?>

My question is why is $ip filter is being shown as invalid but $ip3 when I assign the IP manually the $ip3 filter is being shown as valid. 我的问题是,为什么在我手动分配IP时$ ip过滤器显示为无效,但$ ip3却显示为无效。 Help me please? 请帮帮我?

The command line output may contain trailing spaces or line breaks. 命令行输出可能包含尾随空格或换行符。

Try 尝试

$ip = trim($ip);

before doing the validation. 在进行验证之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM