Skip to content

ip_in_range script IPv6 address has a problem with a IPv6 range #17

@C0nw0nk

Description

@C0nw0nk

So with the ip_in_range functions i tested both IPv4 and IPv6 and IPv4 seems to be functioning fine but IPv6 seems to have a problem with the IP and range i provided it.

IP and range provided.

$ip = "2a02:c7f:9e03:1d00:59c2:3083:c23b:7146";
$range_ip = "2a02:c78::/29";

Also yes i did add my own function to make it easier to use for ipv4 and ipv6 so it can tell the difference between the two. (hopefully it gets added to the main script for easier use)

function ip_range_type($range_ip)
{
    if (strpos($range_ip, ':') !== false) {
        //if ip range provided is ipv6 then
        //echo "range provided is ipv6";
        return 1;
    } else {
        //if ip range provided is ipv4 then
        //echo "range provided is ipv4";
        return 2;
    }
}

function ip_in_range($ip, $range_ip)
{
    //only check IPv4 addresses against IPv4 ranges
    if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && ip_range_type($range_ip) == 2) {
        return ipv4_in_range($ip, $range_ip);
    }
    //only check IPv6 addresses against IPv6 ranges
    if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && ip_range_type($range_ip) == 1) {
        return ipv6_in_range($ip, $range_ip);
    }
}

Online example :
https://3v4l.org/h5psU

I don't know why that range is incorrect if you look up the details on that users IP it provides that as a valid range.
http://ipinfo.io/2a02:c7f:9e03:1d00:59c2:3083:c23b:7146

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions