Current Location: Home> Latest Articles> Want to Retrieve All IPv4 Addresses Corresponding to a Domain? The gethostbynamel Function Makes It Easy

Want to Retrieve All IPv4 Addresses Corresponding to a Domain? The gethostbynamel Function Makes It Easy

gitbox 2025-06-15

In PHP, we often need to obtain the IPv4 addresses corresponding to a domain. This is a common requirement for many application scenarios, such as retrieving a website's IP address for network requests or performing domain resolution and access control on servers. PHP offers a very convenient function—gethostbynamel(), which can quickly return all IPv4 addresses corresponding to a domain.

This article will introduce how to use the gethostbynamel() function to retrieve the IPv4 addresses of a specified domain, with practical examples.

What is the gethostbynamel() function?

gethostbynamel() is a built-in PHP function that accepts a domain name as a parameter and returns an array containing all IPv4 addresses for that domain. This function uses DNS queries to resolve the domain and returns the corresponding IPv4 addresses via A records. Note that the function returns an array, where each element is an IPv4 address.

Function Prototype

array gethostbynamel ( string $hostname )
  • $hostname: The domain to be resolved.

  • Return value: If successful, it returns an array containing IPv4 addresses. If failed, it returns false.

Example: How to Use gethostbynamel() to Get a Domain's IPv4 Addresses

Suppose we need to retrieve all IPv4 addresses for the domain gitbox.net. We can use the following code:

<?php
$domain = "gitbox.net"; // Domain
<p>// Use gethostbynamel to get all IPv4 addresses<br>
$ipv4Addresses = gethostbynamel($domain);</p>
<p>// Check if addresses were successfully retrieved<br>
if ($ipv4Addresses !== false) {<br>
echo "The IPv4 addresses for the domain {$domain} are:\n";<br>
foreach ($ipv4Addresses as $address) {<br>
echo $address . "\n";<br>
}<br>
} else {<br>
echo "Unable to resolve the IPv4 addresses for the domain {$domain}.\n";<br>
}<br>
?><br>

Sample Output:

The IPv4 addresses for the domain gitbox.net are:
192.168.1.1
192.168.1.2

Why Is gethostbynamel() Better than gethostbyname()?

In PHP, we can also use the gethostbyname() function to get a single IPv4 address. However, if a domain has multiple IPv4 addresses (e.g., multiple A records are configured in DNS), gethostbyname() will only return one address. On the other hand, the gethostbynamel() function will return all IPv4 addresses associated with that domain, making it more suitable for scenarios where you need all the addresses.

For example, the code using gethostbyname() looks like this:

<?php
$domain = "gitbox.net";
$ipv4Address = gethostbyname($domain);
echo "The IPv4 address for the domain {$domain} is: {$ipv4Address}\n";
?>

If the domain has multiple A records, the code above will only return one address, while gethostbynamel() ensures you get all IPv4 addresses.

Considerations

  1. DNS Caching: PHP’s DNS query results are cached, so when repeatedly querying the same domain, the returned result may come from the cache. You can ensure you get the latest resolution by restarting the PHP service or using a DNS refresh strategy.

  2. IPv6 Support: gethostbynamel() only returns IPv4 addresses. If you need to get a domain’s IPv6 address, you can use the getaddrinfo() function or use other functions like gethostbyname6().

  3. Error Handling: If gethostbynamel() fails to resolve the domain, it returns false, so appropriate error handling should be implemented in your code to avoid unexpected issues.

  4. Performance Considerations: DNS queries are network operations and relatively slow. If you need to frequently resolve the same domain, you can consider caching the resolution results to avoid repeated DNS queries.

Conclusion

gethostbynamel() is a very convenient and practical function that helps developers easily retrieve all IPv4 addresses for a domain. If you need to get multiple IP addresses, using this function is more efficient and accurate compared to the traditional gethostbyname(). By understanding its usage and considerations, developers can better utilize it for domain resolution tasks in real-world projects.