Archive

Posts Tagged ‘REMOTE_ADDR’

Get The IP Address Of A Visitor Through PHP

November 12th, 2008 1 comment

I have talked previously about getting an IP address of a visitor with PHP. The failing in using the value of $_SERVER['REMOTE_ADDR'] is that if the visitor is using a proxy then you will get the proxy IP address and not the visitors real IP address.

This function works by going through any variables in the $_SERVER array that might exist that would contain information to do with IP addresses. If they are all empty then the function finally looks at $_SERVER['REMOTE_ADDR'] value and returns this as a default.

function getRealIpAddr(){
 if ( !empty($_SERVER['HTTP_CLIENT_IP']) ) {
  //check ip from share internet
  $ip = $_SERVER['HTTP_CLIENT_IP'];
 } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
  //to check ip is pass from proxy
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
 } else {
  $ip = $_SERVER['REMOTE_ADDR'];
 }
 return $ip;
}

To run this function just call it.

echo getRealIpAddr();

This function was originally found here.

Categories: PHP Tags: , , , , ,

How To Read A Remote IP Address In PHP

January 16th, 2008 No comments

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.

Categories: PHP Tags: , , ,