What is the best method to get the users IP address? I've seen a couple of codes on the Internet to get the users IP address, like these:
$ipaddress = $_SERVER["REMOTE_ADDR"];
$ipaddress = getenv('REMOTE_ADDR');
$ipaddress = @$REMOTE_ADDR;
and even this
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
That is the one I use, but to my knowledge there is no way to guarantee the IP you get. I wouldn't rely on it.
You should never ever use anything other then "$SERVER["REMOTEADDR"]" alright. That function "getRealIpAddr" DO NOT USE THAT! The only time you shoulde use "HTTP_CLIENT_IP" or "HTTP_X_FORWARDED_FOR" is when you are behind a known proxy or load balancer. Under any other condition you must use "$SERVER["REMOTEADDR"]" only.
Repeat: If your server is not behind a proxy or load balancer, only ever use "$SERVER["REMOTEADDR"]"
HTTP_CLIENTT_IP, HTTP_X_FORWARDED_FOR, are both user defined and can contain garbage data.
This topic is now closed. New replies are no longer allowed.