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

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

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 `;
echo $ns;

7 thoughts on “Get IP address using gethostbyname PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *