我是如何使用PHP获取本地系统IP地址的

时间:2010-12-16 03:51:25

标签: php

我通过互联网而不是通过本地网络访问网络服务器然后如何获取我本地系统的IP地址我已经尝试了这个

GATEWAY_INTERFACE = <?php echo $_SERVER['GATEWAY_INTERFACE']; ?> <br>
SERVER_ADDR = <?php echo $_SERVER['SERVER_ADDR']; ?> <br>
SERVER_NAME = <?php echo $_SERVER['SERVER_NAME']; ?> <br>
SERVER_SOFTWARE = <?php echo $_SERVER['SERVER_SOFTWARE']; ?> <br>
SERVER_PROTOCOL = <?php echo $_SERVER['SERVER_PROTOCOL']; ?> <br>
REQUEST_METHOD = <?php echo $_SERVER['REQUEST_METHOD']; ?> <br>
REQUEST_TIME = <?php echo $_SERVER['REQUEST_TIME']; ?> <br>
QUERY_STRING = <?php echo $_SERVER['QUERY_STRING']; ?> <br>
DOCUMENT_ROOT = <?php echo $_SERVER['DOCUMENT_ROOT']; ?> <br>
HTTP_ACCEPT = <?php echo $_SERVER['HTTP_ACCEPT']; ?> <br>
HTTP_ACCEPT_CHARSET = <?php echo $_SERVER['HTTP_ACCEPT_CHARSET']; ?> <br>
HTTP_ACCEPT_ENCODING = <?php echo $_SERVER['HTTP_ACCEPT_ENCODING']; ?> <br>
HTTP_ACCEPT_LANGUAGE = <?php echo $_SERVER['HTTP_ACCEPT_LANGUAGE']; ?> <br>
HTTP_CONNECTION = <?php echo $_SERVER['HTTP_CONNECTION']; ?> <br>
HTTP_HOST = <?php echo $_SERVER['HTTP_HOST']; ?> <br>
HTTP_REFERER = <?php echo $_SERVER['HTTP_REFERER']; ?> <br>
HTTP_USER_AGENT = <?php echo $_SERVER['HTTP_USER_AGENT']; ?> <br>
HTTPS = <?php echo $_SERVER['HTTPS']; ?> <br>
REMOTE_ADDR = <?php echo $_SERVER['REMOTE_ADDR']; ?> <br>
REMOTE_HOST = <?php echo $_SERVER['REMOTE_HOST']; ?> <br>
REMOTE_PORT = <?php echo $_SERVER['REMOTE_PORT']; ?> <br>
SCRIPT_FILENAME = <?php echo $_SERVER['SCRIPT_FILENAME']; ?> <br>
SERVER_ADMIN = <?php echo $_SERVER['SERVER_ADMIN']; ?> <br>
SERVER_PORT = <?php echo $_SERVER['SERVER_PORT']; ?> <br>
SERVER_SIGNATURE = <?php echo $_SERVER['SERVER_SIGNATURE']; ?> <br>
PATH_TRANSLATED = <?php echo $_SERVER['PATH_TRANSLATED']; ?> <br>
SCRIPT_NAME = <?php echo $_SERVER['SCRIPT_NAME']; ?> <br>
REQUEST_URI = <?php echo $_SERVER['REQUEST_URI']; ?> <br>
PHP_AUTH_DIGEST = <?php echo $_SERVER['PHP_AUTH_DIGEST']; ?> <br>
PHP_AUTH_USER = <?php echo $_SERVER['PHP_AUTH_USER']; ?> <br>
PHP_AUTH_PW = <?php echo $_SERVER['PHP_AUTH_PW']; ?> <br>
AUTH_TYPE = <?php echo $_SERVER['AUTH_TYPE']; ?> <br>


( and )

<html>
<head>
 <title>What is my IP address?</title>
</head>
<body>
<?php

    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $pipaddress = getenv(HTTP_X_FORWARDED_FOR);
        $ipaddress = getenv(REMOTE_ADDR);
echo "Your Proxy IPaddress is : ".$pipaddress. "(via $ipaddress)" ;
    } else {
        $ipaddress = getenv(REMOTE_ADDR);
        echo "Your IP address is : $ipaddress";
    }
?>
</body>
</html>  

但没有使用它只是给服务器IP。请指导我

1 个答案:

答案 0 :(得分:0)

$ _ SERVER ['REMOTE_ADDR']不适用于负载均衡的网站。为此,以及其他不同情况,请使用此类:

http://brettic.us/code/php-codeigniter-remote-ip-from-load-balancer/

    class RemoteIP
{
 function ip_address()
 {
  if ($this->ip_address !== FALSE)
  {
   return $this->ip_address;
  }

  if ($this->server('HTTP_X_CLIENTIP'))
  {
    $this->ip_address = $_SERVER['HTTP_X_CLIENTIP'];
  }
  elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP'))
  {
    $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
  }
  elseif ($this->server('REMOTE_ADDR'))
  {
    $this->ip_address = $_SERVER['REMOTE_ADDR'];
  }
  elseif ($this->server('HTTP_CLIENT_IP'))
  {
    $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
  }
  elseif ($this->server('HTTP_X_FORWARDED_FOR'))
  {
    $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
  }

  if ($this->ip_address === FALSE)
  {
   $this->ip_address = '0.0.0.0';
   return $this->ip_address;
  }

  if (strstr($this->ip_address, ','))
  {
   $x = explode(',', $this->ip_address);
   $this->ip_address = end($x);
  }

  if ( ! $this->valid_ip($this->ip_address))
  {
   $this->ip_address = '0.0.0.0';
  }

  return $this->ip_address;
 }
}