php 如何在PHP中识别服务器IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5800927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to identify server IP address in PHP
提问by pooja
How can I identify the server IP address in PHP?
如何在PHP中识别服务器IP地址?
回答by CloudyMarble
Like this for the server ip:
像这样的服务器ip:
$_SERVER['SERVER_ADDR'];
and this for the port
这对于港口
$_SERVER['SERVER_PORT'];
回答by John K
If you are using PHP version 5.3 or higher you can do the following:
如果您使用的是 PHP 5.3 或更高版本,您可以执行以下操作:
$host= gethostname();
$ip = gethostbyname($host);
This works well when you are running a stand-alone script, not running through the web server.
当您运行独立脚本而不是通过 Web 服务器运行时,这很有效。
回答by Flask
for example:
例如:
$_SERVER['SERVER_ADDR']
when your on IIS, try:
当您在 IIS 上时,请尝试:
$_SERVER['LOCAL_ADDR']
回答by Dom
I came to this page looking for a way of getting my own ip address not the one of the remote machine connecting to me.
我来到这个页面寻找一种方法来获取我自己的 IP 地址,而不是连接到我的远程机器的 IP 地址。
This will not work for a windows machine.
这不适用于 Windows 机器。
But in case someone searches for what I was looking for:
但万一有人搜索我要找的东西:
#! /usr/bin/php
<?php
$my_current_ip=exec("ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'");
echo $my_current_ip;
(Shamelessly adapted from How to I get the primary IP address of the local machine on Linux and OS X?)
(无耻地改编自How to I get the primary IP address of the local machine on Linux and OS X?)
回答by Ben D
Neither of the most up-voted answers will reliably return the server's public address. Generally $_SERVER['SERVER_ADDR']
will be correct, but if you're accessing the server via a VPN it will likely return the internal network address rather than a public address, and even when not on the same network some configurations will will simply be blank or have some other specified value.
投票最多的答案都不会可靠地返回服务器的公共地址。通常$_SERVER['SERVER_ADDR']
是正确的,但如果您通过 VPN 访问服务器,它可能会返回内部网络地址而不是公共地址,即使不在同一网络上,某些配置也将只是空白或具有其他指定价值。
Likewise, there are scenarios where $host= gethostname(); $ip = gethostbyname($host);
won't return the correct values because it's relying on on both DNS (either internally configured or external records) and the server's hostname settings to extrapolate the server's IP address. Both of these steps are potentially faulty. For instance, if the hostname of the server is formatted like a domain name (i.e. HOSTNAME=yahoo.com
) then (at least on my php5.4/Centos6 setup) gethostbyname
will skip straight to finding Yahoo.com's address rather than the local server's.
同样,在某些情况下$host= gethostname(); $ip = gethostbyname($host);
不会返回正确的值,因为它依赖 DNS(内部配置或外部记录)和服务器的主机名设置来推断服务器的 IP 地址。这两个步骤都有潜在的错误。例如,如果服务器的主机名格式类似于域名(即HOSTNAME=yahoo.com
),那么(至少在我的 php5.4/Centos6 设置中)gethostbyname
将直接跳到查找 Yahoo.com 的地址而不是本地服务器的地址。
Furthermore, because gethostbyname
falls back on public DNS records a testing server with unpublished or incorrect public DNS records (for instance, you're accessing the server by localhost
or IP address, or if you're overriding public DNS using your local hosts
file) then you'll get back either no IP address (it will just return the hostname) or even worse it will return the wrong address specified in the public DNS records if one exists or if there's a wildcard for the domain.
此外,因为gethostbyname
依靠公共 DNS 记录测试服务器的未发布或不正确的公共 DNS 记录(例如,您通过localhost
IP 地址访问服务器,或者如果您使用本地hosts
文件覆盖公共 DNS ),那么您要么没有IP地址(它只会返回主机名),或者更糟糕的是,如果存在公共DNS记录或域有通配符,它将返回在公共DNS记录中指定的错误地址。
Depending on the situation, you can also try a third approach by doing something like this:
根据情况,您还可以通过执行以下操作来尝试第三种方法:
$external_ip = exec('curl http://ipecho.net/plain; echo');
This has its own flaws (relies on a specific third-party site, and there could be network settings that route outbound connections through a different host or proxy) and like gethostbyname
it can be slow. I'm honestly not sure which approach will be correct most often, but the lesson to take to heart is that specific scenarios/configurations will result in incorrect outputs for all of these approaches... so if possible verify that the approach you're using is returning the values you expect.
这有其自身的缺陷(依赖于特定的第三方站点,并且可能存在通过不同主机或代理路由出站连接的网络设置)并且gethostbyname
速度可能很慢。老实说,我不确定哪种方法最常是正确的,但是要牢记的教训是,特定的场景/配置会导致所有这些方法的输出不正确……因此,如果可能,请验证您使用的方法using 正在返回您期望的值。
回答by Emmanuel Mahuni
This is what you could use as an adaptation of the above examples without worrying about curl installed on your server.
这是您可以用作上述示例的改编版本,而无需担心服务器上安装的 curl。
<?php
// create a new cURL resource
$ch = curl_init ();
// set URL and other appropriate options
curl_setopt ($ch, CURLOPT_URL, "http://ipecho.net/plain");
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$ip = curl_exec ($ch);
echo "The public ip for this server is: $ip";
// close cURL resource, and free up system resources
curl_close ($ch);
?>
回答by Stefano Zanola
If you are using PHP in bash shell you can use:
如果您在 bash shell 中使用 PHP,则可以使用:
$server_name=exec('hostname');
Because $_SERVER[] SERVER_ADDR
, HTTP_HOST
and SERVER_NAME
are not set.
因为$_SERVER[] SERVER_ADDR
,HTTP_HOST
和SERVER_NAME
没有设置。
回答by qbert220
The previous answers all give $_SERVER['SERVER_ADDR']. This will not work on some IIS installations. If you want this to work on IIS, then use the following:
前面的答案都给出了 $_SERVER['SERVER_ADDR']。这不适用于某些 IIS 安装。如果您希望它在 IIS 上运行,请使用以下命令:
$server_ip = gethostbyname($_SERVER['SERVER_NAME']);
回答by Sanford Staab
I found this to work for me: GetHostByName("");
我发现这对我有用:GetHostByName("");
Running XAMPP v1.7.1 on Windows 7 running Apache webserver. Unfortunately it just give my gateway IP address.
在运行 Apache 网络服务器的 Windows 7 上运行 XAMPP v1.7.1。不幸的是,它只提供了我的网关 IP 地址。