Skip to content

filter_var FILTER_VALIDATE_IP with ip '0.2.3.4' will return true #11098

@joe820912boy

Description

@joe820912boy

Description

The following code:

<?php
$ip = '0.2.3.4'; // replace with the IP address you want to validate

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

Resulted in this output:

0.2.3.4 is a valid IP address

But I expected this output instead:

not a valid IP address

I check this code on https://onlinephp.io/c/b1510

I think that '0.2.3.4' shouldn't be a valid ip address with 0 in the head.

But the filter_var will return true for this error ip

PHP Version

8.2.5

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions