denasio — 2012-09-05T05:52:20-04:00 — #1
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
beingtomgreen — 2012-09-05T08:12:30-04:00 — #2
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.
logic_earth — 2012-09-05T17:23:55-04:00 — #3
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.