Browse Source

[HttpFoundation] added Request::getClientIp()

Fabien Potencier 14 years ago
parent
commit
15cd2643c0
1 changed files with 20 additions and 0 deletions
  1. 20 0
      src/Symfony/Component/HttpFoundation/Request.php

+ 20 - 0
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.
      *