Is comparing the parameter passed to gethostbyname with its return value a reliable method to determine host availability?
Comparing the parameter passed to gethostbyname with its return value is not a reliable method to determine host availability. This is because gethostbyname may return the IP address even if the host is not reachable. To reliably check host availability, you can use functions like fsockopen to establish a connection to the host and check if it is reachable.
function isHostAvailable($host) {
$fp = @fsockopen($host, 80, $errno, $errstr, 5);
if ($fp) {
fclose($fp);
return true;
} else {
return false;
}
}
$host = "example.com";
if (isHostAvailable($host)) {
echo "Host is available";
} else {
echo "Host is not available";
}