W3docs

PHP gethostbyaddr() Function: Everything You Need to Know

As a PHP developer, you may need to obtain the host name of a given IP address. In such scenarios, the PHP gethostbyaddr() function comes in handy. It is a

When you have an IP address and want to know the host name behind it, PHP's gethostbyaddr() function does the job. It performs a reverse DNS lookup — the opposite of resolving a domain name to an IP. This article covers its syntax, return value, common use cases, and the gotchas that trip people up.

What is the gethostbyaddr() Function?

The gethostbyaddr() function is a built-in PHP function that performs a reverse DNS (rDNS) lookup to retrieve the host name registered for a given IP address. It relies on your system's DNS resolver, so the result depends on the network the script runs on and on the IP owner having configured a PTR record.

Key facts to keep in mind:

  • It accepts an IPv4 or IPv6 address as a string.
  • On success, it returns the host name as a string.
  • On failure to resolve, it returns the original IP address string unchanged — not false. That is the main behavioral surprise, and it shapes how you must check the result.
  • On an invalid argument (a string that is not a valid IP), it returns false and emits a warning.

A reverse lookup only succeeds if the owner of the IP block has published a PTR record. Many addresses (including most residential and many cloud IPs) have no PTR or one that does not match the forward record, so a "missing" host name is normal, not an error in your code.

Syntax

gethostbyaddr(string $ip): string|false
ParameterTypeDescription
$ipstringThe IPv4 or IPv6 address to look up.

Return value: the host name on success, the unchanged $ip string when no host name could be found, or false if $ip is not a valid address.

Basic Example

Because the function returns the IP itself when resolution fails, you compare the result against the input to detect failure:

php— editable, runs on the server

Here 8.8.8.8 (Google Public DNS) resolves to a host name such as dns.google. The three-way check distinguishes an invalid input (false), an unresolved address (returns the IP), and a successful lookup.

Resolving the Visitor's Host Name

A common use is logging or analytics: turning a visitor's IP into a readable host name.

<?php

$ip = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
$host = gethostbyaddr($ip);

echo $host === $ip
    ? "Visitor IP $ip has no reverse DNS entry."
    : "Visitor came from $host.";
?>

Do not use the result for access control. Reverse DNS can be spoofed by whoever controls the IP's PTR record, so a host name like example.com proves nothing on its own. For trusted forward-confirmed reverse DNS (FCrDNS), look up the host name with gethostbyaddr() and then confirm it resolves back to the same IP with gethostbyname().

Round-Tripping with gethostbyname()

gethostbyaddr() and gethostbyname() are inverses. You can use them together to verify that a host name and IP genuinely match:

<?php

$ip = "8.8.8.8";
$host = gethostbyaddr($ip);
$confirmed = gethostbyname($host) === $ip;

echo "Host: $host\n";
echo "Forward-confirmed: " . ($confirmed ? "yes" : "no") . "\n";
?>

When $confirmed is yes, both directions of the DNS lookup agree — the strongest signal you get from plain DNS that the host name is legitimate.

Common Gotchas

  • A returned IP is not an error. Always check $result === $ip rather than assuming a non-false result is a host name.
  • Lookups are slow and blocking. Each call may wait on a network round trip. Avoid calling it in a tight loop on every request; cache results where possible.
  • Validate input first. If the value may not be a valid IP, run it through filter_var() with FILTER_VALIDATE_IP before calling gethostbyaddr().
  • Results vary by environment. The same IP can return different host names (or none) depending on the DNS servers configured where the script runs.
  • gethostbyname() — resolve a host name to an IPv4 address (the forward lookup).
  • gethostbynamel() — get the full list of IPv4 addresses for a host name.
  • gethostname() — get the host name of the local machine.
  • checkdnsrr() — check whether DNS records of a given type exist for a host.
  • ip2long() — convert an IPv4 address into its integer representation.

Conclusion

The gethostbyaddr() function performs a reverse DNS lookup, turning an IP address into a host name. The crucial detail is its return behavior: it gives back the original IP when no host name is found and false only for invalid input, so check the result carefully. Pair it with gethostbyname() when you need forward-confirmed reverse DNS, and never trust an unverified reverse lookup for security decisions.

Practice

Practice
What does the gethostbyaddr() function in PHP do?
What does the gethostbyaddr() function in PHP do?
Was this page helpful?