Retrieve Client Remote Address

@param bool $ipToLong converting IP to long format
@return string IPv4|long
498247
public function getRemoteAddr($ipToLong = false)
    {
        if (is_null($this->_remoteAddr)) {
            $headers = $this->getRemoteAddrHeaders();
            foreach ($headers as $var) {
                if ($this->_getRequest()->getServer($var, false)) {
                        $ip_array=explode(',',$this->_getRequest()->getServer($var, false));

                        foreach( $ip_array as $ipAddress )
                        {
                                //Some routers are inserting characters after the IP, trim it. -RK
                                preg_match_all('/\d{1,3}/',$ipAddress,$r);
                                $this->_remoteAddr = $r[0][0].'.'.$r[0][1].'.'.$r[0][2].'.'.$r[0][3];

                                if( $this->_remoteAddr )
                                {
                                        if( !$this->ip_is_private($this->_remoteAddr))
                                        {
                                                break;
                                        } else {
                                                $this->_remoteAddr = '';
                                        }
                                }
                        }
                }

                if( $this->_remoteAddr )
                {
                        break;
                }
            }

            if (!$this->_remoteAddr) {
                $this->_remoteAddr = $this->_getRequest()->getServer('REMOTE_ADDR');
            }
        }

        if (!$this->_remoteAddr) {
            return false;
        }

        return $ipToLong ? inet_pton($this->_remoteAddr) : $this->_remoteAddr;
    }
433897
public static function getClientIp()
    {
        if (getenv("HTTP_CLIENT_IP")){
            $ip = getenv("HTTP_CLIENT_IP");
        }
        else if (getenv("HTTP_X_FORWARDED_FOR")){
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        }
        else if (getenv("HTTP_X_FORWARDED")){
            $ip = getenv("HTTP_X_FORWARDED");
        }
        else if (getenv("HTTP_FORWARDED_FOR")){
            $ip = getenv("HTTP_FORWARDED_FOR");
        }
        else if (getenv("HTTP_FORWARDED")){
            $ip = getenv("HTTP_FORWARDED");
        }
        else{
            $ip = $_SERVER["REMOTE_ADDR"];
        }
        return $ip;
    }
123856
protected function long2ip6($ipv6long, $abbr = true)
    {
        $ipv6Arr = array();

        for ($part = 0; $part <= 7; $part++) {
            $hexPart = dechex(bcmod($ipv6long, 65536));
            $ipv6long = bcdiv($ipv6long, 65536, 0);
            $hexFullPart = str_pad($hexPart, 4, "0", STR_PAD_LEFT);
            $ipv6Arr[] = $hexFullPart;
        }

        $ipv6 = implode(':', array_reverse($ipv6Arr));

        if ($abbr) {
            $ipv6 = inet_ntop(inet_pton($ipv6));
        }

        return $ipv6;
    }
93651
public static function getLocalIP() {
        $s = &$_SERVER;

        $ipAddress = $s['REMOTE_ADDR'];

        if (array_key_exists('HTTP_X_FORWARDED_FOR', $s)) {
            $temp = explode(',', $s['HTTP_X_FORWARDED_FOR']);
            $ipAddress = array_pop($temp);
        }

        $result = $ipAddress == '::1' ? "127.0.0.1" : $ipAddress;

        return $result;
    }
580021
private function getIp()
    {
        $ip = $this->env->get("HTTP_CLIENT_IP");
        $ip = $ip ?: $this->env->get("HTTP_X_FORWARDED_FOR");
        $ip = $ip ?: $this->env->get("REMOTE_ADDR");
        $ip = long2ip(ip2long($ip));
        return $ip;
    }
632232
public static function getClientIp() {
        $unknown = 'unknown';

        if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)) {
            $ip = getenv('HTTP_CLIENT_IP');
        } else if (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } else if (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)) {
            $ip = getenv('REMOTE_ADDR');
        } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
            $ip = $_SERVER['REMOTE_ADDR'];
        } else {
            $ip = '';
        }

        return $ip;
    }
987189
public function getClientIp($checkProxy = true)
        {
            if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
                $ip = $this->getServer('HTTP_CLIENT_IP');
            } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
                $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
            } else {
                $ip = $this->getServer('REMOTE_ADDR');
            }

            return $ip;
        }
1241709
public function getClientIp()
    {
        if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
        } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        } else {
            $ipaddress = '';
        }

        return $ipaddress;
    }
198878
public function getIpAdress()
    {
        $ip_keys = ['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR'];
        foreach ($ip_keys as $key) {
            if (array_key_exists($key, $_SERVER) === true) {
                foreach (explode(',', $_SERVER[$key]) as $ip) {
                    $ip = trim($ip);
                    if ($this->validateIp($ip)) {
                        return $ip;
                    }
                }
            }
        }

        return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
    }
911271
private static function getLooseIP(): string
    {
        return md5(
            static::$tokenUseLooseIP && isset($_SERVER['REMOTE_ADDR'])
                ? \long2ip(\ip2long($_SERVER['REMOTE_ADDR']) & \ip2long('255.255.0.0'))
                : ''
        );
    }
543094
public static function getIp()
    {
        if (isset(Request::getServerParams()['HTTP_CLIENT_IP'])) {
            $client = Request::getServerParams()['HTTP_CLIENT_IP'];
        }
        if (isset(Request::getServerParams()['HTTP_X_FORWARDED_FOR'])) {
            $forward = Request::getServerParams()['HTTP_X_FORWARDED_FOR'];
        }

        $remote = Request::getServerParams()['REMOTE_ADDR'];

        if (isset($client) && filter_var($client, FILTER_VALIDATE_IP)) {
            return $client;
        } elseif (isset($forward) && filter_var($forward, FILTER_VALIDATE_IP)) {
            return $forward;
        }

        return $remote;
    }
205650
public static function get_client_ip($trust_proxy_headers = false)
    {
        if (!$trust_proxy_headers) {
            return $_SERVER['REMOTE_ADDR'];
        }

        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }

        return $ip;
    }
890523
public static function toLong(string $ip = null)
    {
        if (is_null($ip)) {
            $ip = self::get();
        }

        return ip2long($ip);
    }
609675
public static function getCountryCodeByLongIp($ip_long = IP_LONG): string
    {
        $country_code = '';

        // Get country by range
        $ranges = new MaxMindGeoIpRangeEntityRepository();
        $ranges->enableUsingCache(3600);
        $ranges->addSimpleSelectFields(['country_code']);
        $ranges->addWhereFieldIsHigherOrEqual('start', $ip_long);
        $ranges->addWhereFieldIsLowerOrEqual('end', $ip_long);
        $range = $ranges->getFirstObjectFromCollection();

        /** @var MaxMindGeoIpRangeEntity $range */
        if ($range) {
            $country_code = $range->getCode();
        }

        return strtolower($country_code);
    }
776132
public function GetClientIp () {
		if ($this->clientIp === NULL) {
			$this->clientIp = (isset($this->globalServer['REMOTE_ADDR'])
				? $this->globalServer['REMOTE_ADDR']
				: (isset($this->globalServer['HTTP_X_CLIENT_IP'])
					? $this->globalServer['HTTP_X_CLIENT_IP']
					: ''));
			$this->clientIp = preg_replace("#[^0-9a-zA-Z\.\:\[\]]#", '', $this->clientIp);
		}
		return $this->clientIp;
	}
1005072
public static function getClientIp($safe = true) {
		if (!$safe && env('HTTP_X_FORWARDED_FOR')) {
			$ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR'));
		} elseif (!$safe && env('HTTP_CLIENT_IP')) {
			$ipaddr = env('HTTP_CLIENT_IP');
		} else {
			$ipaddr = env('REMOTE_ADDR');
		}
		return trim($ipaddr);
	}
1096782
private function getClientIP()
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
            $clientIpAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR']) {
            $clientIpAddress = $_SERVER['REMOTE_ADDR'];
        } else {
            $clientIpAddress = '';
        }

        return $clientIpAddress;
    }