Converting IP Addresses and Hostnames

Even if your server does not provide you with a $_SERVER['REMOTE_HOST'] variable, you will probably know the IP address of a visitor from the $_SERVER['REMOTE_ADDR'] environment variable. You can use this in conjunction with the function gethostbyaddr() to get the user’s hostname. gethostbyaddr() requires a string representing an IP address and returns the equivalent hostname. If an error occurs, it returns the IP address it was given. Listing 14.5 creates a script that uses gethostbyaddr() to acquire the user’s hostname if the $REMOTE_HOST variable is unavailable.

Listing 14.5. Using gethostbyaddr() to Get a Hostname
 1: <!DOCTYPE html PUBLIC 2: "-//W3C//DTD XHTML 1.0 Strict//EN" 3: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.