Revision c3b3e9c7
Added by Stilez y about 10 years ago
etc/inc/util.inc | ||
---|---|---|
615 | 615 |
|
616 | 616 |
/* returns true if $ipaddr is a valid dotted IPv4 address */ |
617 | 617 |
function is_ipaddrv4($ipaddr) { |
618 |
if (!is_string($ipaddr) || empty($ipaddr)) { |
|
619 |
return false; |
|
620 |
} |
|
621 |
|
|
622 |
$ip_long = ip2long($ipaddr); |
|
623 |
$ip_reverse = long2ip32($ip_long); |
|
624 |
|
|
625 |
if ($ipaddr == $ip_reverse) { |
|
626 |
return true; |
|
627 |
} else { |
|
618 |
if (!is_string($ipaddr) || empty($ipaddr) || ip2long($ipaddr) === FALSE) { |
|
628 | 619 |
return false; |
629 | 620 |
} |
621 |
return true; |
|
630 | 622 |
} |
631 | 623 |
|
632 | 624 |
/* returns true if $ipaddr is a valid IPv6 linklocal address */ |
Also available in: Unified diff
simplify is_ipaddrv4() and fix zero-padding issue
Fixes these two issues:
1) The historical workaround of testing IPv4 for validity by (a) converting to long (b) converting back again, then (c) comparing to see if it's the same as the original, is redundant. The old issue with ip2long() was fixed in PHP 5.2.10 and invalid IPv4 can now be tested simply by ip2long() === FALSE.
2) The workaround didn't really work optimally anyway as it mis-reported otherwise valid IPs as invalid if any octet or the IP as a whole was zero padded. Some IP lists or IP data sources users might use could be zero padded - an avoidable headache.