Home > Backend Development > PHP Tutorial > PHP check and verify that the IP is a valid IP address function_PHP tutorial

PHP check and verify that the IP is a valid IP address function_PHP tutorial

WBOY
Release: 2016-07-13 16:57:43
Original
1691 people have browsed it

Before php5.2, if we want to verify whether the IP address is valid, we need to use regular expressions to verify whether the IP address is legal. If it is valid, we need to call ping to operate, but after php5.2.0, there is a special Functions are used to make this determination. Let me summarize these functions

to determine whether it is a legal IP

The code is as follows
 代码如下 复制代码
if(filter_var($ip, FILTER_VALIDATE_IP)) {// it's valid
}else {// it's not valid
}
Copy code

if(filter_var($ip, FILTER_VALIDATE_IP)) {// it's valid

}else {// it's not valid

}
 代码如下 复制代码
 
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {// it's valid
}else {// it's not valid
}

Determine whether it is a legal IPv4 IP address

 代码如下 复制代码
 
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) {// it's valid
}else {// it's not valid
}

The code is as follows Copy code
 代码如下 复制代码

 
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) {// it's valid
}else {// it's not valid
}

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {// it's valid
 代码如下 复制代码

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {// it's valid
}else {// it's not valid
}

}else {// it's not valid

}

 代码如下 复制代码

//判断IP格式
 function is_ip($gonten){
        $ip = explode(".",$gonten);
        for($i=0;$i {
if($ip[$i]>255){
               return (0);
               }
        }
        return ereg("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$",$gonten);
}

Determine whether it is a legal public IPv4 address. Private IP addresses such as 192.168.1.1 will be excluded
The code is as follows Copy code
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) {// it's valid }else {// it's not valid } Determine whether it is a legal IPv6 address
The code is as follows Copy code
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE)) {// it's valid }else {// it's not valid } Determine whether it is a public IPv4 IP or a legal Public IPv6 IP address
The code is as follows Copy code
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {// it's valid }else {// it's not valid } If your PHP version is too low, none of the above functions can be used, but we can use regular expressions to verify
The code is as follows Copy code
//Determine IP format function is_ip($gonten){           $ip = explode(".",$gonten); for($i=0;$i           {<🎜> If($ip[$i]>255){                  return (0);                 } } return ereg("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$" ,$gonten); }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631494.htmlTechArticleBefore php5.2, if we want to verify whether the IP address is valid, we need to use regular expressions to verify that only the IP address is It is not legal. If it is to be effective, we need to call ping to operate, but...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template