PHP中如何识别服务器IP地址

发布于 2024-11-03 20:44:24 字数 23 浏览 0 评论 0原文

PHP中如何识别服务器IP地址?

How can I identify the server IP address in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

墨小沫ゞ 2024-11-10 20:44:24

对于服务器 ip 来说是这样的:

$_SERVER['SERVER_ADDR'];

对于端口也是如此

$_SERVER['SERVER_PORT'];

Like this for the server ip:

$_SERVER['SERVER_ADDR'];

and this for the port

$_SERVER['SERVER_PORT'];
和我恋爱吧 2024-11-10 20:44:24

如果您使用的是 PHP 5.3 或更高版本,您可以执行以下操作:

$host= gethostname();
$ip = gethostbyname($host);

当您运行独立脚本而不是通过 Web 服务器运行时,此方法效果很好。

If you are using PHP version 5.3 or higher you can do the following:

$host= gethostname();
$ip = gethostbyname($host);

This works well when you are running a stand-alone script, not running through the web server.

吝吻 2024-11-10 20:44:24

例如:

$_SERVER['SERVER_ADDR']

当您在 IIS 上时,请尝试:

$_SERVER['LOCAL_ADDR']

for example:

$_SERVER['SERVER_ADDR']

when your on IIS, try:

$_SERVER['LOCAL_ADDR']
囚你心 2024-11-10 20:44:24

得票最多的答案都不会可靠地返回服务器的公共地址。通常 $_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 记录(如果存在)或者该域是否存在通配符。

根据情况,您还可以尝试第三种方法,方法如下:

$external_ip = exec('curl http://ipecho.net/plain; echo');

这有其自身的缺陷(依赖于特定的第三方站点,并且可能存在通过不同主机或代理路由出站连接的网络设置)和 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 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.

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.

月隐月明月朦胧 2024-11-10 20:44:24

我来到这个页面寻找一种获取我自己的 IP 地址而不是连接到我的远程计算机的 IP 地址的方法。

这不适用于 Windows 机器。

但万一有人搜索我正在寻找的内容:(

#! /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;

无耻地改编自如何在 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:

#! /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?)

谁把谁当真 2024-11-10 20:44:24

您可以将其用作上述示例的改编版本,而不必担心服务器上安装的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);
    ?>

This is what you could use as an adaptation of the above examples without worrying about curl installed on your server.

<?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);
    ?>
绿光 2024-11-10 20:44:24

前面的答案都给出了$_SERVER['SERVER_ADDR']。这在某些 IIS 安装上不起作用。如果您希望它在 IIS 上运行,请使用以下命令:

$server_ip = gethostbyname($_SERVER['SERVER_NAME']);

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_ip = gethostbyname($_SERVER['SERVER_NAME']);
埋情葬爱 2024-11-10 20:44:24

检查 $_SERVER 数组

echo $_SERVER['SERVER_ADDR'];

Check the $_SERVER array

echo $_SERVER['SERVER_ADDR'];
抱着落日 2024-11-10 20:44:24

如果您在 bash shell 中使用 PHP,则可以使用:

$server_name=exec('hostname');

因为未设置 $_SERVER[] SERVER_ADDRHTTP_HOSTSERVER_NAME

If you are using PHP in bash shell you can use:

$server_name=exec('hostname');

Because $_SERVER[] SERVER_ADDR, HTTP_HOST and SERVER_NAME are not set.

对风讲故事 2024-11-10 20:44:24

我发现这对我有用:
获取主机名(“”);

在运行 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.

无言温柔 2024-11-10 20:44:24

我刚刚创建了一个简单的脚本,它将带回 IIS 中的 $_SERVER['REMOTE_ADDR'] 和 $_SERVER['SERVER_ADDR'],因此您不必更改每个变量。只需将此文本粘贴到每个页面中包含的 php 文件中即可。

/** IIS IP Check **/
if(!$_SERVER['SERVER_ADDR']){ $_SERVER['SERVER_ADDR'] = $_SERVER['LOCAL_ADDR']; }
if(!$_SERVER['REMOTE_ADDR']){ $_SERVER['REMOTE_ADDR'] = $_SERVER['LOCAL_ADDR']; }

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.

/** IIS IP Check **/
if(!$_SERVER['SERVER_ADDR']){ $_SERVER['SERVER_ADDR'] = $_SERVER['LOCAL_ADDR']; }
if(!$_SERVER['REMOTE_ADDR']){ $_SERVER['REMOTE_ADDR'] = $_SERVER['LOCAL_ADDR']; }
半世晨晓 2024-11-10 20:44:24

要准确获取外部IP地址,您可以调用Amazon提供的服务checkip.amazonaws.com。

$ip = exec("curl https://checkip.amazonaws.com");

正如我们所看到的,这是一个非常普遍的问题,以至于 AWS 创造了这个工具供全世界使用。

我会使用它而不是其他类似的服务,因为它是由 AWS 提供的,并且可能会长期存在。

附加

对于专业用途,我建议使用上面亚马逊的 checkip。但要在终端进行快速而肮脏的检查,您可以使用 https://ip.me,这是第三方到目前为止,该服务对我来说一直运行良好:

curl ip.me

如果他们检测到这是一个 cli 工具连接,它只会返回没有 HTML 标记的 IP。

To accurately get the external IP address, you can call checkip.amazonaws.com, a service provided by Amazon.

$ip = exec("curl https://checkip.amazonaws.com");

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:

curl ip.me

If they detect it's a cli tool connecting, it just returns the IP with no HTML markup.

鼻尖触碰 2024-11-10 20:44:24
$serverIP = $_SERVER["SERVER_ADDR"];
echo "Server IP is: <b>{$serverIP}</b>";
$serverIP = $_SERVER["SERVER_ADDR"];
echo "Server IP is: <b>{$serverIP}</b>";
ま柒月 2024-11-10 20:44:24

如果您没有从上述答案中得到任何信息并且您使用的是旧版本的 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

孤单情人 2024-11-10 20:44:24

您可以使用 https://icanhazip.com 并且从 Cloudflare 拥有这个项目 有时它甚至可以更可靠。

$my_real_ip = file_get_contents('https://icanhazip.com/');

You can use https://icanhazip.com and since Cloudflare owned this project sometimes it can be even more reliable.

$my_real_ip = file_get_contents('https://icanhazip.com/');
紅太極 2024-11-10 20:44:24

像这样:

$_SERVER['SERVER_ADDR'];

Like this:

$_SERVER['SERVER_ADDR'];
白色秋天 2024-11-10 20:44:24

检查 $_SERVER 数组

echo $_SERVER['SERVER_ADDR'];

Check the $_SERVER array

echo $_SERVER['SERVER_ADDR'];
终陌 2024-11-10 20:44:24

当站点在负载均衡器、反向代理服务器或 CloudFront 之类的 CDN 后面运行时,这是一种解决方案:

$conn = curl_init();
curl_setopt($conn, CURLOPT_URL, 'any.valid.url');
curl_exec($conn);
$WebServerIP = curl_getinfo($conn)['local_ip'];

Here is one solution when the site is running behind a load-balancer, a reverse proxy server, or a CDN like CloudFront:

$conn = curl_init();
curl_setopt($conn, CURLOPT_URL, 'any.valid.url');
curl_exec($conn);
$WebServerIP = curl_getinfo($conn)['local_ip'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文