Get Visitors Real IP Address in PHP
If you want track visitor's or user's IP address than you may use $_SERVER['REMOTE_ADDR']. Well, you might be shocked to know that it may not return the real IP address of the visitor/user at all time. Here are the cases when it happens. If visitor/user is connected to the Internet through Proxy Server then $_SERVER['REMOTE_ADDR'] in PHP will give the IP address of the proxy server and it will not be visitor's/user's IP address.
In PHP , there are other server variables ( HTTP_CLIENT_IP , HTTP_X_FORWARDED_FOR along with REMOTE_ADDR) using which we can find the real IP address even if visitor/user is connected to Proxy Server. Here is a function in PHP to find the real IP address of the visitor's/user's PC.
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
In above PHP function, it wil first check for IP address of visitor's/user's PC and return if it get IP address. If client's IP address is not available then it will find for forwarded for IP address using HTTP_X_FORWARDED_FOR. If still it did not get IP address than it return IP address using REMOTE_ADDR.