Incorrect visitor IP recorded when using http proxy fix
-
Update: I think you can ignore this post, by the looks of it ppl with a http proxy can set their own http header name for the ip address so it’s up to them to do it correctly. So it’s not a bug. The ones I used appear to be pretty common though (I found them used in various articles) so you may want to use them anyway 😉
I installed HAProxy to do some load balancing and all of a sudden all of my visitors were reported to be using my proxy server’s IP. Turns out your get_ip() function didn’t get all header possibilites. So I added 2 more. Not sure if we’ve got all of them now but I think so. Here’s the new function(s):
function get_ip_lazy($key, $use_getenv = false) { $ip = ""; if(!$use_getenv) { if (isset($_SERVER[$key]) && luc_ip_not_private($_SERVER[$key])) $ip = $_SERVER[$key]; } else { $val = ""; $val = @getenv($key); //Dirty, maybe not even necessary. In the original code it said: if(getenv($key) && luc_ip_not_private(getenv($key))) : which is something I don't understand because the one implies it's a bool and the other implies it's a string.. if ($val != "" && luc_ip_not_private($val)) $ip = $val; } return $ip; } function luc_get_ip() { $DoVars = Array('HTTP_X_REAL_IP', 'HTTP_X_CLIENT', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR'); $use_getenv = false; if ($_SERVER) $use_getenv = false; else $use_getenv = true; $ip = ""; foreach($DoVars as $aVar) { $ip = get_ip_lazy($aVar, $use_getenv); if($ip != "") break; } return $ip; }Note the
$val = @getenv($key);line.. not sure if that’s okay to do but what you were doing looked even weirder.
The topic ‘Incorrect visitor IP recorded when using http proxy fix’ is closed to new replies.