Flipkart

Friday, February 26, 2010

How to get the Actual IP address of client

get the IP address from your PHP code using the HTTP_X_FORWARDED_FOR field from $_SERVER variable. For example the below code. It will display the client's actual IP address, if it is available.


$ip= $REMOTE_ADDR;
GetHostByName($REMOTE_ADDR);

OR

if( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ){
    print $_SERVER['HTTP_X_FORWARDED_FOR'];
}
 OR

   getenv('REMOTE_ADDR');


Sample output : 192.168.1.56

The above code will only print the IP address, if the x-forwarded-for header is updated by the gateway of client. Otherwise it is not possible to detect the thing. If 100 users are browsing your page, it can happen that 10 of them you can get the actual IP address, and others are only showing the gateway IP address.

No comments:

Post a Comment