Website Scripts And Cloud Tutorials

Get IP address using gethostbyname PHP

Byadmin

Feb 27, 2009 , ,

Last updated on February 9th, 2022 at 11:54 am

Click to rate this tutorial!
[Total: 3 Average: 3.3]

gethostbyname() is used to get the IP address corresponding to a given host name. For example if you would like to find the ip address of say google.com

<?php
$ip_ad=gethostbyname('www.google.com');
echo $ip_ad;
?>

This function will return the IPv4 address of the hostname provided.

Bonus, Find the name server associated with the hostname using the code below.
Note: This will only work in Linux / Unix systems in which you have dig utility installed. If dig is not in the /usr/bin/dig location make sure to change that. (Not tested in Windows)

$ns = `/usr/bin/dig +short NS google.com `;
echo $ns;
Click to rate this tutorial!
[Total: 3 Average: 3.3]
7 thoughts on “Get IP address using gethostbyname PHP”

Leave a Reply

Your email address will not be published.