Tuesday, September 1, 2009

Verifying a hostname / IP address with PHP

If you ever need to verify a hostname or an IP address in PHP, here's how: // try to determine the IP address of the hostname // if the hostname is actually an IP, gethostbyname() will return it unchanged // if the hostname cannot be resolved, it will have the same behavior $ip = gethostbyname($address); // check if the resulting IP is valid if ($ip !== long2ip(ip2long($ip))) { echo "Invalid hostname or IP address"; }

No comments:

Post a Comment