How to get visitor’s IP address?

Hi all friend.

I wrote a PHP function, which will get visitors IP address. The good thing in this function is that, it will also get IP address behind Proxy server. You can see the detailed PHP codes. This function has intelligent codes. I hope it will help for PHP developers. My recommendation is that do not use only $_SERVER[“REMOTE_ADDR”]. Sometime it doesn’t return correct IP address of visitors.

function myip() {
	if (getenv('HTTP_CLIENT_IP')) $ip = getenv('HTTP_CLIENT_IP');
	elseif (getenv('HTTP_X_FORWARDED_FOR')) $ip = getenv('HTTP_X_FORWARDED_FOR');
	elseif (getenv('HTTP_X_FORWARDED')) $ip = getenv('HTTP_X_FORWARDED');
	elseif (getenv('HTTP_FORWARDED_FOR')) $ip = getenv('HTTP_FORWARDED_FOR');
	elseif (getenv('HTTP_FORWARDED')) $ip = getenv('HTTP_FORWARDED');
	else $ip = $_SERVER['REMOTE_ADDR'];
	return $ip;