[HttpFoundation] added Request::getClientIp()

This commit is contained in:
Fabien Potencier 2010-09-01 13:56:20 +02:00
parent f3caaf9638
commit 15cd2643c0

View File

@ -262,6 +262,26 @@ class Request
$this->session = $session;
}
/**
* Returns the client IP address.
*
* @param Boolean $proxy Whether the current request has been made behind a proxy or not
*
* @return string The client IP address
*/
public function getClientIp($proxy = false)
{
if ($proxy) {
if ($this->server->has('HTTP_CLIENT_IP')) {
return $this->server->get('HTTP_CLIENT_IP');
} elseif ($this->server->has('HTTP_X_FORWARDED_FOR')) {
return $this->server->get('HTTP_X_FORWARDED_FOR');
}
}
return $this->server->get('REMOTE_ADDR');
}
/**
* Returns current script name.
*