in PHP how can i determine if an IP address string is a localhost/loopback address or not? my attempt:
function isLocalhost(string $ip): bool
{
if ($ip === "::1") {
// IPv6 localhost (technically ::1/128 is the ipv6 loopback range, not sure how to check for that, bcmath probably)
return true;
}
$long = @ip2long($ip);
if ($long !== false && ($long >> 24) === 127) {
// IPv4 loopback range 127.0.0.0/8
return true;
}
return false;
}
I use a function for that:
You can also use regex to catch ranges