PHP method to determine whether it is a valid IP address

小云云
Release: 2023-03-19 17:44:01
Original
5270 people have browsed it

When most people see this blog, their first impression must be that it talks about how to judge through regular expressions. This article mainly introduces the method of determining whether an IP is a valid IP address in PHP. Friends who need it can refer to it. I hope it can help everyone.

No, after php5.2.0, there is a special function to make this judgment.

Judge whether it is a legal IP


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

Judge whether it is a legal IPv4 IP address


if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
// it's valid
}
else {
// it's not valid
}
Copy after login

Determine whether it is a legal public IPv4 address. Private IP addresses such as 192.168.1.1 will be excluded


if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) {
// it's valid
}
else {
// it's not valid
}
Copy after login

Determine whether it is a legal IPv6 address


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

Determine whether it is a public IPv4 IP or a legal Public IPv6 IP address


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
}
Copy after login

Related recommendations :

How to calculate IP address mask in php

IP address processing in PHP

php How to limit the IP address range

The above is the detailed content of PHP method to determine whether it is a valid IP address. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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