Tutorial :Fail-proof method to get external (non-private) IP address on CakePHP?



Question:

The getClientIP( ) method that comes with CakePHP is returning private IPs sometimes. For example IPs like 127.0.0.1 or 192.x.x.x. I assume this is caused by proxies.

Looking at the getClientIP( ) code it does seem like it tries to deal with proxies issues but it doesn't really check if the IP is private or not.

What would be the best way to get the real, non-private, IP address?

Thanks.


Solution:1

Try calling getClientIp(false). If that doesn't work, you could just use $_SERVER["REMOTE_ADDR"] directly instead. It will always contain the external IP that actually connected to the server. This address can't be manipulated, unlike http headers intended for proxies.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »