PHP中如何识别服务器IP地址
PHP中如何识别服务器IP地址?
How can I identify the server IP address in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
PHP中如何识别服务器IP地址?
How can I identify the server IP address in PHP?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(18)
对于服务器 ip 来说是这样的:
对于端口也是如此
Like this for the server ip:
and this for the port
如果您使用的是 PHP 5.3 或更高版本,您可以执行以下操作:
当您运行独立脚本而不是通过 Web 服务器运行时,此方法效果很好。
If you are using PHP version 5.3 or higher you can do the following:
This works well when you are running a stand-alone script, not running through the web server.
例如:
当您在 IIS 上时,请尝试:
for example:
when your on IIS, try:
得票最多的答案都不会可靠地返回服务器的公共地址。通常
$_SERVER['SERVER_ADDR']
是正确的,但如果您通过 VPN 访问服务器,它可能会返回内部网络地址而不是公共地址,即使不在网络上同一网络的某些配置将只是空白或具有其他指定值。同样,在某些情况下
$host= gethostname(); $ip = gethostbyname($host);
不会返回正确的值,因为它依赖 DNS(内部配置或外部记录)和服务器的主机名设置来推断服务器的 IP 地址。这两个步骤都可能存在错误。例如,如果服务器的主机名的格式类似于域名(即HOSTNAME=yahoo.com
),那么(至少在我的 php5.4/Centos6 设置上)gethostbyname
将直接跳到查找 Yahoo.com 的地址而不是本地服务器的地址。此外,由于
gethostbyname
依靠公共 DNS 记录,测试服务器具有未发布或不正确的公共 DNS 记录(例如,您通过localhost
或 IP 地址访问服务器,或者如果您使用本地hosts
文件覆盖公共 DNS),那么您将无法返回任何 IP 地址(它只会返回主机名),或者更糟糕的是,它将返回中指定的错误地址公共 DNS 记录(如果存在)或者该域是否存在通配符。根据情况,您还可以尝试第三种方法,方法如下:
这有其自身的缺陷(依赖于特定的第三方站点,并且可能存在通过不同主机或代理路由出站连接的网络设置)和 gethostbyname 一样,它可能会很慢。老实说,我不确定哪种方法最常见,但要记住的教训是,特定的场景/配置将导致所有这些方法的输出不正确......因此,如果可能的话,请验证您所使用的方法using 正在返回您期望的值。
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.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.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 bylocalhost
or IP address, or if you're overriding public DNS using your localhosts
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.Depending on the situation, you can also try a third approach by doing something like this:
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.我来到这个页面寻找一种获取我自己的 IP 地址而不是连接到我的远程计算机的 IP 地址的方法。
这不适用于 Windows 机器。
但万一有人搜索我正在寻找的内容:(
无耻地改编自如何在 Linux 和 OS X 上获取本地计算机的主 IP 地址?)
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.
This will not work for a windows machine.
But in case someone searches for what I was looking for:
(Shamelessly adapted from How to I get the primary IP address of the local machine on Linux and OS X?)
您可以将其用作上述示例的改编版本,而不必担心服务器上安装的curl。
This is what you could use as an adaptation of the above examples without worrying about curl installed on your server.
前面的答案都给出了$_SERVER['SERVER_ADDR']。这在某些 IIS 安装上不起作用。如果您希望它在 IIS 上运行,请使用以下命令:
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 数组
Check the $_SERVER array
如果您在 bash shell 中使用 PHP,则可以使用:
因为未设置
$_SERVER[] SERVER_ADDR
、HTTP_HOST
和SERVER_NAME
。If you are using PHP in bash shell you can use:
Because
$_SERVER[] SERVER_ADDR
,HTTP_HOST
andSERVER_NAME
are not set.我发现这对我有用:
获取主机名(“”);
在运行 Apache Web 服务器的 Windows 7 上运行 XAMPP v1.7.1。
不幸的是它只给出了我的网关IP地址。
I found this to work for me:
GetHostByName("");
Running XAMPP v1.7.1 on Windows 7 running Apache webserver.
Unfortunately it just give my gateway IP address.
我刚刚创建了一个简单的脚本,它将带回 IIS 中的 $_SERVER['REMOTE_ADDR'] 和 $_SERVER['SERVER_ADDR'],因此您不必更改每个变量。只需将此文本粘贴到每个页面中包含的 php 文件中即可。
I just created a simple script that will bring back the $_SERVER['REMOTE_ADDR'] and $_SERVER['SERVER_ADDR'] in IIS so you don't have to change every variable. Just paste this text in your php file that is included in every page.
要准确获取外部IP地址,您可以调用Amazon提供的服务checkip.amazonaws.com。
正如我们所看到的,这是一个非常普遍的问题,以至于 AWS 创造了这个工具供全世界使用。
我会使用它而不是其他类似的服务,因为它是由 AWS 提供的,并且可能会长期存在。
附加:
对于专业用途,我建议使用上面亚马逊的 checkip。但要在终端进行快速而肮脏的检查,您可以使用 https://ip.me,这是第三方到目前为止,该服务对我来说一直运行良好:
如果他们检测到这是一个 cli 工具连接,它只会返回没有 HTML 标记的 IP。
To accurately get the external IP address, you can call checkip.amazonaws.com, a service provided by Amazon.
As we can see it's such a widespread problem that AWS has created that very tool for the world to use.
I would use that over other similar services, since it's provided by AWS and will probably be around for long.
Additional:
For professional use, I would recommend using checkip from Amazon above. But for a quick and dirty check at the terminal, you can use https://ip.me, that's a third party service has been working great for me so far:
If they detect it's a cli tool connecting, it just returns the IP with no HTML markup.
如果您没有从上述答案中得到任何信息并且您使用的是旧版本的 PHP,您可能必须使用
$HTTP_SERVER_VARS['server_ADDR']
You may have to use
$HTTP_SERVER_VARS['server_ADDR']
if you are not getting anything from above answers and if you are using older version of PHP您可以使用 https://icanhazip.com 并且从 Cloudflare 拥有这个项目 有时它甚至可以更可靠。
You can use https://icanhazip.com and since Cloudflare owned this project sometimes it can be even more reliable.
像这样:
Like this:
检查 $_SERVER 数组
Check the $_SERVER array
当站点在负载均衡器、反向代理服务器或 CloudFront 之类的 CDN 后面运行时,这是一种解决方案:
Here is one solution when the site is running behind a load-balancer, a reverse proxy server, or a CDN like CloudFront: