From 15cd2643c04a1441db3476aa5d09ff05e9c0d83f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 1 Sep 2010 13:56:20 +0200 Subject: [PATCH] [HttpFoundation] added Request::getClientIp() --- .../Component/HttpFoundation/Request.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 55a8f2fd1a..340a5f815d 100755 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -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. *