How To Read A Remote IP Address In PHP

PHP keeps certain variables to do with server and networking in an associative array called SERVER. To find out the remote address of a user you can use the array identifier REMOTE_ADDR. This is used in the following manner.

$ipaddress = $_SERVER['REMOTE_ADDR'];

This IP address can be passed into the gethostbyaddr() function to find out host name associated with the specified IP address.

$hostname = gethostbyaddr($ip);

You can then pass this into a database on your site to record not only who is visiting your site but where they are from. Gaining this information from PHP is beneficial if you are using Google Analytics as Google doesn't tell you specific visitors just an overall picture. Additionally, Google Analytics will only record information from users who have JavaScript turned on. You will find that there might be a lot of traffic to your site that isn't recorded like search engine spiders and page scrapers.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
2 + 16 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.