Get client IP address

We know in PHP, we use $_SERVER["REMOTE_ADDR"] to retrieve the client IP address. We will see how to get the client IP address in Laravel.

Request::ip();
is the method in Laravel. It uses the Symphony Request Object’s getClientIps method.

public function getClientIps()

{

    $clientIps = array();

    $ip = $this->server->get('REMOTE_ADDR');

    if (!$this->isFromTrustedProxy()) {

        return array($ip);

    }

    if (self::$trustedHeaders[self::HEADER_FORWARDED] && $this->headers->  has(self::$trustedHeaders[self::HEADER_FORWARDED])) {

        $forwardedHeader = $this->headers->get(self::$trustedHeaders[self::HEADER_FORWARDED]);

        preg_match_all('{(for)=("?\[?)([a-z0-9\.:_\-/]*)}', $forwardedHeader, $matches);

        $clientIps = $matches[3];

    } elseif (self::$trustedHeaders[self::HEADER_CLIENT_IP] && $this->headers->has(self::$trustedHeaders[self::HEADER_CLIENT_IP])) {

        $clientIps = array_map('trim', explode(',', $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_IP])));

    }

    $clientIps[] = $ip; // Complete the IP chain with the IP the request actually came from

    $ip = $clientIps[0]; // Fallback to this when the client IP falls into the range of trusted proxies

    foreach ($clientIps as $key => $clientIp) {

        // Remove port (unfortunately, it does happen)

        if (preg_match('{((?:\d+\.){3}\d+)\:\d+}', $clientIp, $match)) {

            $clientIps[$key] = $clientIp = $match[1];

        }

        if (IpUtils::checkIp($clientIp, self::$trustedProxies)) {

            unset($clientIps[$key]);

}

    }

    // Now the IP chain contains only untrusted proxies and the client IP

    return $clientIps ? array_reverse($clientIps) : array($ip);

}