This is the code iam using for validating the domain name using gethostbyname().
This is working fine until i use it on localhost.
As soon as i upload it on my server,gethostbyname() started returning ip address of the unknown domain name as well.
$url=$_GET['d'];
function getHost($Address) {
$parseUrl = parse_url(trim($Address));
return trim($parseUrl['host'] ? $parseUrl['host'] : array_shift(explode('/', $parseUrl['path'], 2)));
}
$get=getHost($url);
$domain = str_ireplace('www.', '', $get);
if(filter_var(gethostbyname($domain), FILTER_VALIDATE_IP))
{
echo gethostbyname($domain);
echo $domain;
}
else
{
echo gethostbyname($domain);
echo "Not Valid";
}
My test cases are :-
1)www- it return 184.173.134.234
2)google.coma- it returns 67.215.65.132
3)google.comaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - it returns 67.215.65.132
One more interesting thing happening is that for these same domain name when i run them on my localhost .this same function returns Not valid.
Any help would be appreciated :)
Based on the answer and research by Ben D...
# Get the URL from the GET Parameters
$url = $_GET['d'];
# Derive the HTTP Host
$hostname = parse_url( $url , PHP_URL_HOST );
# Strip the "WWW." from the hostname if present
$hostname = preg_replace( '/^www\./i' , '' , $hostname );
# Look for the IP Address
$ip_address = gethostbyname( $hostname );
if( filter_var( $hostname , FILTER_VALIDATE_IP ) ){
echo 'Hostname is an IP Address already';
echo $hostname;
}elseif( $ip_address==$hostname || $ip_address=='67.215.65.132' ){
echo 'Domain Not Found';
}elseif( filter_var( $ip_address , FILTER_VALIDATE_IP ) ){
echo $ip_address;
echo $hostname;
}else{
echo 'Invalid IP Address Returned';
echo $ip_address;
echo $hostname;
}