Php checking and verifying IP addresses as valid IP address function _ PHP Tutorial

Source: Internet
Author: User
Php checks and verifies that the IP address is a valid IP address. Before php5.2, if we want to verify whether the IP address is valid, we need to use regular expressions to verify that the IP address is valid. if it is valid, we need to call ping, but before php5.2, if we want to verify whether the IP address is valid, we need to use regular expressions to verify that the IP address is valid. if it is valid, we need to call ping, but after php5.2.0, there are special functions to make this judgment. I will summarize these functions below

Determine whether the IP address is valid

The code is as follows:
If (filter_var ($ ip, FILTER_VALIDATE_IP) {// it's valid
} Else {// it's not valid
}

Determine whether an IPv4 IP address is valid

The code is as follows:

If (filter_var ($ ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) {// it's valid
} Else {// it's not valid
}

Check 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:

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 an IPv6 address is valid

The code is as follows:


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 address or a legal Public IPv6 IP address

The code is as follows:

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, the above functions cannot be used, but we can use regular expression for verification.

The code is as follows:

// Determine the 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 );
}

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.