PHP can be used to get the IP Address of a website visitor, IP address of the server hosting the website and resolve the IP address to get the hostname a.k.a reverse DNS records. PHP has predefined $_SERVER array variable using which IP addresses of both visitors and host can be retrieved. The gethostbyaddr() function is used to find out the hostname. The indices REMOTE_ADDR and HTTP_HOST contain the visitor IP address and host IP addressTo find out the IP address of a visitor using PHP use the $_SERVER[‘REMOTE_ADDR’] variable. For example, the following code
<?php print "Your IP address is ".$_SERVER['REMOTE_ADDR']; ?>
will display the visitor IP address to the visitor himself. If you want to record this IP address insert the value into the database.
To know your web server’s IP address using PHP use the $_SERVER[‘HTTP_HOST’] variable.
To get the hostname of the variable use the gethostbyaddr() function with IP address as the parameter. For example
<?php print gethostbyaddr("127.0.0.1"); ?>
will print localhost. If a hostname cannot be resolved the same IP address is returned.
Pratik says
Thanks Nice website 🙂
Daquan E Wright says
Thanks……using this to aid in a script I’m writing for my job!