New on LowEndTalk? Please Register and read our Community Rules.
Strange PHP Issue
I have CentMinMod running on a server.
<? function getRealIpAddr() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } return $ip; } echo getRealIpAddr(); ?>
Basically it returns this.
MyIPaddress, MyIPAddress
so like
99.XX.XX.XXX,99.XX.XX.XXX
Anyone know why it does that? I only need my IP to show once, because it's causing problems with an application I am coding.
Comments
Is this the full code or just part? Reason I ask is because may the function is getting called twice from somewhere else.
HTTP_X_FORWARDED_FOR is getting populated with it.
Do print_r($_SERVER); and you'll see the same thing from it.
Francisco
thats the full php code in that file.
return explode(',', $ip)[0];
EDIT: This might not be so good answer. It just takes the first IP.
here is the output of your command. what do you recommend?
http://pastebin.com/crW8nduf
What @drazilox said isn't bad, but you'll likely want to skip the first entry since it's sometimes 127.0.0.1.
Francisco
All right. Thanks for the quick help everyone.
Added in
return explode(',', $ip)[1];
I'm not sure if HTTP_X_FORWARDED_FOR always returns multiple IPs. When it doensn't, then that fails. Maybe it's better to grab the last one.
end(explode(',', $ip));
EDIT: Check the comments on this http://stackoverflow.com/a/24275009
What proxy do you have in front of it?
nginx reverse proxy on another server with this config
Basically
Cloudflare --> OVH VPS Reverse Proxy --> VPS with website on it
http://phptester.net/ Test your PHP code online without the need of a web server. (PHP 5.2, 5.3 and 5.4).