Ip address validation in php

❮ PHP Filter Reference

Example

Check if the variable $ip is a valid IP address:

$ip = "127.0.0.1";

if (filter_var($ip, FILTER_VALIDATE_IP)) {
    echo("$ip is a valid IP address");
} else {
    echo("$ip is not a valid IP address");
}
?>

Try it Yourself »


Definition and Usage

The FILTER_VALIDATE_IP filter validates an IP address.

Possible flags:

  • FILTER_FLAG_IPV4 - The value must be a valid IPv4 address
  • FILTER_FLAG_IPV6 - The value must be a valid IPv6 address
  • FILTER_FLAG_NO_PRIV_RANGE - The value must not be within a private range
  • FILTER_FLAG_NO_RES_RANGE - The value must not be within a reserved range

More Examples

Example 1

Check if the variable $ip is a valid IPv6 address:

$ip = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";

if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
    echo("$ip is a valid IPv6 address");
} else {
    echo("$ip is not a valid IPv6 address");
}
?>

Try it Yourself »


❮ PHP Filter Reference


I want to check if an entered input is a valid IP address or not. I would like a specific function that will help me validate a users input.

Ip address validation in php

hakre

187k48 gold badges419 silver badges802 bronze badges

asked Jun 2, 2011 at 6:19

3

filter_var($ip, FILTER_VALIDATE_IP)

http://www.php.net/filter_var

Example:-

if(filter_var($ip, FILTER_VALIDATE_IP)){
  echo 'Valid IP';
} else {
  echo 'Not Valid IP';
}

user229044

225k40 gold badges323 silver badges333 bronze badges

answered Jun 2, 2011 at 6:23

decezedeceze

497k81 gold badges718 silver badges865 bronze badges

4

// Usually you'd get the value from $_POST or $_GET
$ip = "10.3.1.5";
if(!filter_var($ip, FILTER_VALIDATE_IP)) {
   echo "Not a valid IP address!";
}

You can modify this by filtering for IPv4 and IPv6 IP addresses and exclude private and reserved IPs.

http://www.php.net/manual/filter.filters.validate.php

answered Jun 2, 2011 at 6:26

chiborgchiborg

25.6k12 gold badges97 silver badges113 bronze badges

By using preg_match();

function checkIPAddress($ipAddress) 
{
    return preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ipAddress);
}

answered Aug 3, 2015 at 9:45

Ip address validation in php

Aniket SinghAniket Singh

2,2541 gold badge19 silver badges30 bronze badges

1

FILTER_VALIDATE_IP can be used to validate IP address ( both IPv4 and IPv6 ) . This filter id = 275. Here is the code.
$ip="255.255.47.12";
if(filter_var($ip,FILTER_VALIDATE_IP)){
echo " Correct , validation passed";
}else{
echo " Wrong , validation failed ";
}
Filter FlagsDESCRIPTION
FILTER_FLAG_IPV4 Only IPV4 formats are allowed
FILTER_FLAG_IPV6 Only IPV6 formats are allowed
FILTER_FLAG_NO_PRIV_RANGE Private range of IPV4 & IPV6 are not allowed.
IPV4 :10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16
IPV6: Starting with FD or FC
FILTER_FLAG_NO_RES_RANGE Reserved range of IPV4 & IPV6 are not allowed.
IPV4 :0.0.0.0/8, 169.254.0.0/16, 127.0.0.0/8 and 240.0.0.0/4
IPV6: ::1/128, ::/128, ::ffff:0:0/96 and fe80::/10

To check IPV6 address ONLY, we can add optional flag like this

$ip='2002:0001:3238:DFE1:0063:0000:0000:FEFB'; //
if(filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_IPV6)){
echo " Correct ,   IP Validation passed ";
}else{
echo " Wrong , IP Validation failed ";
}

Output is

Correct , IP Validation passed

To check IPV4 address ONLY , we can add optional flag like this

$ip='2002:0001:3238:DFE1:0063:0000:0000:FEFB'; //
if(filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_IPV4)){
echo " Correct ,   IP Validation passed ";
}else{
echo " Wrong , IP Validation failed ";
}

Above validation will fail as we used FILTER_FLAG_IPV4 to check one IPV6 address.
Now let us try one IPV4 address.

$ip='127.0.0.0'; //Change this value to check different IP
if(filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_IPV4)){
echo " Correct ,   IP Validation passed ";
}else{
echo " Wrong , IP Validation failed ";
}

Output is here

Correct , IP Validation passed

No private range of IP is allowed by using Flag FILTER_FLAG_NO_PRIV_RANGE

$ip='10.0.0.0'; //Change this value to check different IP
if(filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_NO_PRIV_RANGE)){
echo " Correct ,   IP Validation passed ";
}else{
echo " Wrong , IP Validation failed ";
}

Output is

Wrong , IP Validation failed

No reserve range of IP address are allowed by using FILTER_FLAG_NO_RES_RANGE

$ip='169.254.0.0'; //Change this value to check different IP
if(filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_NO_RES_RANGE)){
echo " Correct ,   IP Validation passed ";
}else{
echo " Wrong , IP Validation failed ";
}

Output is

Wrong , IP Validation failed

Ip address validation in php


Ip address validation in php

plus2net.com

How validate IP in PHP?

The FILTER_VALIDATE_IP filter validates an IP address. Possible flags: FILTER_FLAG_IPV4 - The value must be a valid IPv4 address. FILTER_FLAG_IPV6 - The value must be a valid IPv6 address.

How do I check if an IP address is valid?

Master C and Embedded C Programming- Learn as you go.
Tokenize the string (IP address) using the dot “.” delimiter..
If the sub strings are containing any non-numeric character, then return false..
If the number in each token is not in range 0 to 255, then return false..

What is IP address in PHP?

The simplest way to collect the visitor IP address in PHP is the REMOTE_ADDR. Pass the 'REMOTE_ADDR' in PHP $_SERVER variable. It will return the IP address of the visitor who is currently viewing the webpage.

What is the regex for IP address?

\d{1,3}\b will match any IP address just fine. But will also match 999.999. 999.999 as if it were a valid IP address. If your regex flavor supports Unicode, it may even match ١٢٣.