返回访问IP

如果获取请求IP失败,则返回0.0.0.0
@return string
181018
public static function getUserIp()
    {
        static $clientIp;
        
        if ($clientIp) {
            return $clientIp;
        }
        
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
                foreach ($arr as $ip) {
                    $ip = trim($ip);
                    if ($ip != "unknown") {
                        $clientIp = $ip;
                        break;
                    }
                }
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $clientIp = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $clientIp = $_SERVER['REMOTE_ADDR'];
            } else {
                $clientIp = "0.0.0.0";
            }
        } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
            $clientIp = getenv("HTTP_X_FORWARDED_FOR");
        } elseif (getenv("HTTP_CLIENT_IP")) {
            $clientIp = getenv("HTTP_CLIENT_IP");
        } else {
            $clientIp = getenv("REMOTE_ADDR");
        }
        
        preg_match( "/[\\d\\.]{7,15}/", $clientIp, $onlineIp);
        $clientIp = !empty($onlineIp[0]) ? $onlineIp[0] : '0.0.0.0';
        
        $clientIp = preg_replace("/[^0-9\\.]/", '', $clientIp);
        
        return $clientIp;
    }
1205274
public function getClientIp()
    {
        if (!empty($this->getHeader('X-Forwarded-For'))) {
            $ip = preg_replace('/(?:,.*)/', '', $this->hasHeader('X-Forwarded-For'));
        } elseif (!empty($this->getHeader('Client-Ip'))) {
            $ip = $this->getHeader('Client-Ip');
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return trim($ip);
    }
892203
public function getIp()
    {
        if (! $this->ip) {
            if ($this->random) {
                $this->ip = long2ip(mt_rand());
            } else {
                $this->ip = array_get($_SERVER, 'HTTP_CLIENT_IP', array_get($_SERVER, 'HTTP_X_FORWARDED_FOR', array_get($_SERVER, 'HTTP_X_FORWARDED', array_get($_SERVER, 'HTTP_FORWARDED_FOR', array_get($_SERVER, 'HTTP_FORWARDED', array_get($_SERVER, 'REMOTE_ADDR', '127.0.0.1'))))));
            }
        }

        // sometimes we can get multiple ipaddresses
        // seperated with a ',', eg: proxy/vpn
        $this->ip = explode(',', $this->ip);
        $this->ip = trim(head($this->ip));

        return $this->ip;
    }
272245
public function getUserIP()
    {
        if (isset($this->_userIP)) {
            return $this->_userIP;
        }
        return $this->_userIP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
    }
768543
public function getRemoteIP(): string
    {
        $server = $this->server;

        if (!empty($server['HTTP_CLIENT_IP'])) {
            return $server['HTTP_CLIENT_IP'];
        }

        if (!empty($server['HTTP_X_FORWARDED_FOR'])) {
            return $server['HTTP_X_FORWARDED_FOR'];
        }

        return $server['REMOTE_ADDR'];
    }
705385
public static function getIpAddress()
    {
        // check for shared internet/ISP IP
        if (isset($_SERVER['HTTP_CLIENT_IP']) && self::validateIp($_SERVER['HTTP_CLIENT_IP'])) {
            return $_SERVER['HTTP_CLIENT_IP'];
        }

        // check for IPs passing through proxies
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            // check if multiple ips exist in var
            if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false) {
                $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                foreach ($iplist as $ip) {
                    if (self::validateIp($ip)) {
                        return $ip;
                    }
                }
            } else {
                if (self::validateIp($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                    return $_SERVER['HTTP_X_FORWARDED_FOR'];
                }
            }
        }
        if (isset($_SERVER['HTTP_X_FORWARDED']) && self::validateIp($_SERVER['HTTP_X_FORWARDED'])) {
            return $_SERVER['HTTP_X_FORWARDED'];
        }
        if (isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && self::validateIp($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
            return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
        }
        if (isset($_SERVER['HTTP_FORWARDED_FOR']) && self::validateIp($_SERVER['HTTP_FORWARDED_FOR'])) {
            return $_SERVER['HTTP_FORWARDED_FOR'];
        }
        if (isset($_SERVER['HTTP_FORWARDED']) && self::validateIp($_SERVER['HTTP_FORWARDED'])) {
            return $_SERVER['HTTP_FORWARDED'];
        }
        // return unreliable ip since all else failed
        return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown';
    }
735708
public function getRemoteIP(): string
    {
        if (null === $this->remote_ip) {
            $ip = $_SERVER['REMOTE_ADDR'];
            if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                $ip = explode(',', $ip);
                $ip = trim($ip[0]);
            }
            $this->remote_ip = $ip;
        }
        return $this->remote_ip;
    }
852478
public function getIp(){
		if( (int) $this->lastCheck > 0 && time() - $this->lastCheck < 10 * 60 )
			return $this->lastIp;
		$this->reader->setUrl( 'http://checkip.dyndns.org' );
		$html	= $this->reader->read();
		$parts	= explode( ": ", strip_tags( $html ) );
		$ip		= trim( array_pop( $parts ) );
		$this->save( array( 'ip' => $ip, 'timestamp' => time() ) );
		return $ip;
	}
320505
public static function getIp() {
        if (!empty ($_SERVER ["HTTP_CLIENT_IP"])) {
            $cip = $_SERVER ["HTTP_CLIENT_IP"];
        } else if (!empty ($_SERVER ["HTTP_X_FORWARDED_FOR"])) {
            $cip = $_SERVER ["HTTP_X_FORWARDED_FOR"];
            $cip = trim(implode(',', $cip)[0]);
        } else if (!empty ($_SERVER ["REMOTE_ADDR"])) {
            $cip = $_SERVER ["REMOTE_ADDR"];
        } else {
            $cip = "";
        }

        return $cip;
    }
135567
public function getIpAddress()
    {
        foreach ($this->headers as $k)
        {
            if (isset($this->server[$k]))
            {
                // header can be comma separated
                $ips = explode(',', $this->server[$k]);
                $ip = trim(end($ips));
                $ip = filter_var($ip, FILTER_VALIDATE_IP);
                if (false !== $ip)
                {
                    return $ip;
                }
            }
        }

        // no valid ip found
        return false;
    }
533735
public function getClientIp()
    {
        $ipaddress = getenv('HTTP_CLIENT_IP') ?:
            getenv('HTTP_X_FORWARDED_FOR') ?:
                getenv('HTTP_X_FORWARDED') ?:
                    getenv('HTTP_FORWARDED_FOR') ?:
                        getenv('HTTP_FORWARDED') ?:
                            getenv('REMOTE_ADDR');
			    
	//Try to get client IP from SERVER variables
        if(!$ipaddress)
        {
            return $this->getClientIpFromServerVar();
        }
        return $ipaddress;
    }
1321437
protected function getUserIp()
    {
        $client  = @$_SERVER['HTTP_CLIENT_IP'];
        $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
        $remote  = $_SERVER['REMOTE_ADDR'];

        if (filter_var($client, FILTER_VALIDATE_IP)) {
            $ip = $client;
        } elseif (filter_var($forward, FILTER_VALIDATE_IP)) {
            $ip = $forward;
        } else {
            $ip = $remote;
        }

        return $ip;
    }
454247
public function filter_ip( ){

		// get IP
		$ip = $_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'];
		}

		return $ip;

	}
220035
public static function getIpFromHeader()
    {
        $general = Config::getInstance()->General;
        $clientHeaders = @$general['proxy_client_headers'];
        if (!is_array($clientHeaders)) {
            $clientHeaders = array();
        }

        $default = '0.0.0.0';
        if (isset($_SERVER['REMOTE_ADDR'])) {
            $default = $_SERVER['REMOTE_ADDR'];
        }

        $ipString = self::getNonProxyIpFromHeader($default, $clientHeaders);
        return IPUtils::sanitizeIp($ipString);
    }
789340
public static function ip(): string
    {
        if (static::$realIp !== null) {
            return static::$realIp;
        }

        $possibilities = [
          'HTTP_CLIENT_IP',
          'HTTP_X_FORWARDED_FOR',
          'HTTP_X_FORWARDED',
          'HTTP_X_CLUSTER_CLIENT_IP',
          'HTTP_FORWARDED_FOR',
          'HTTP_FORWARDED',
          'REMOTE_ADDR'
        ];

        foreach ($possibilities as $key) {
            if (isset($_SERVER[$key])) {
                foreach (explode(',', $_SERVER[$key]) as $ip) {
                    $ip = trim($ip);

                    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
                        static::$realIp = $ip;
                        return $ip;
                    }
                }
            }
        }

        if (KOLDY_CLI) {
            static::$realIp = '127.0.0.1';
        } else if (isset($_SERVER['REMOTE_ADDR'])) {
            static::$realIp = $_SERVER['REMOTE_ADDR'];
        } else {
            throw new Exception('Unable to detect IP');
        }

        return static::$realIp;
    }
539788
public function GetServerIp () {
		if ($this->serverIp === NULL) {
			$this->serverIp = (isset($this->globalServer['SERVER_ADDR'])
				? $this->globalServer['SERVER_ADDR']
				: (isset($this->globalServer['LOCAL_ADDR'])
					? $this->globalServer['LOCAL_ADDR']
					: ''));
		}
		return $this->serverIp;
	}
390752
public function getCurrentConnectIp()
    {
        try {
            $this->Ip->setHaProxy(TRUE);

            return $this->Ip->getIpAddress();
        }
        catch (\Exception $e) {
            $message = 'Error File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Code: ' . $e->getCode() . ' - Message: ' . $e->getMessage();
            $this->logger->error(__FUNCTION__, $message);

            return '0.0.0.0';
        }
    }