Jon Haworth

PHP: Get remote IP address


Does what it says on the tin. Multiple proxies may cause problems, and I'd like to know how to detect them if anyone fancies enlightening me.


// Using if...else...

if (getenv(HTTP_X_FORWARDED_FOR)) {							
    $ip = getenv(HTTP_X_FORWARDED_FOR); 
} else { 
    $ip = getenv(REMOTE_ADDR);
}	
		
// Using the ternary operator

$ip = (getenv(HTTP_X_FORWARDED_FOR))
    ?  getenv(HTTP_X_FORWARDED_FOR)
    :  getenv(REMOTE_ADDR);
		

More PHP?

Photo