PHP Function GetHostByAddr
Sometimes you need to get the host name of a particular machine/server. To do that you need to use a built-in function in PHP called gethostbyaddr. It’s a simple and easy to use function, which is also very reliable.
It’s a function that allows you to retrieve the human-readable host name associated with an IP address. It does that by performing a reverse DNS lookup on the given IP address.
To use it you just need to pass the IP address to gethostbyaddr and it will return the corresponding hostname on success or a string containing the unmodified IP address on failure. The IP address to lookup should be valid and should not be a private or a reserved address.
You can use it for example in an HTML element to print out a user’s real IP Address instead of their browser’s IP address. This way you can protect users privacy and still show them their real IP address.
Note that if you use this function a lot and/or if your server has to perform lots of lookups then it may cause the server to run slowly. This because gethostbyaddr does a reverse DNS lookup and it uses the system’s DNS service for that. This means that if the server doesn’t have an answer in its cache it will contact one of its upstream servers and so on until it gets an answer. In many cases this could take quite some time, so you should be cautious about using it if your server runs in production.