如何在 PHP(CLI) 中找到我服务器的 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1814611/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:56:00  来源:igfitidea点击:

How do I find my server's IP address in PHP(CLI)

ip-addressphp

提问by ChronoFish

Aside from the obvious (localhost, 127.0.0.1) does PHP (command line interface!) have a mechanism for discovering the IP of the computer the script is running on?

除了明显的(本地主机,127.0.0.1)之外,PHP(命令行界面!)是否有一种机制来发现运行脚本的计算机的 IP?

$_SERVER[*]will not work as this is not a Web app - this is a command line script.

$_SERVER[*]将无法工作,因为这不是 Web 应用程序 - 这是一个命令行脚本。

TIA

TIA

回答by

You can get the hostname by using gethostname

您可以通过使用获取主机名 gethostname

回答by Anish

try this it should return the ip address of the server

试试这个它应该返回服务器的IP地址

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

回答by Matthew Scharley

If you are working with PHP < 5.3, this may help (on *NIX based systems atleast):

如果您使用 PHP < 5.3,这可能会有所帮助(至少在基于 *NIX 的系统上):

 mscharley@S04:~$ cat test.php
#!/usr/bin/env php
<?php

function getIPs($withV6 = true) {
    preg_match_all('/inet'.($withV6 ? '6?' : '').' addr: ?([^ ]+)/', `ifconfig`, $ips);
    return $ips[1];
}

$ips = getIPs();
var_dump($ips);

 mscharley@S04:~$ ./test.php
array(5) {
  [0]=>
  string(13) "72.67.113.141"
  [1]=>
  string(27) "fe80::21c:c0ff:fe4a:d09d/64"
  [2]=>
  string(13) "72.67.113.140"
  [3]=>
  string(9) "127.0.0.1"
  [4]=>
  string(7) "::1/128"
}
 mscharley@S04:~$

Or, if you don't anticipate doing it often, then perhaps this would work (just don't abuse it):

或者,如果您不希望经常这样做,那么也许这会起作用(只是不要滥用它):

$ip = file_get_contents('http://whatismyip.org/');

回答by ImpressTheNet Web Developer

I know this is a fairly old question, but there doesn't seem to be a definitive answer (in as much as one is possible.) I've had a need to determine this value, both on *NIX boxes and on Win X boxes. Also from a CLI executed script as well as a non-CLI script. The following function is the best I've come up with, which borrows on different concepts people have spoke of over the years. Maybe it can be of some use:

我知道这是一个相当古老的问题,但似乎没有明确的答案(尽可能多)。我需要在 *NIX 框和 Win X 上确定这个值盒子。同样来自 CLI 执行的脚本以及非 CLI 脚本。以下函数是我想出的最好的函数,它借鉴了人们多年来谈论的不同概念。也许它可以有一些用处:

function getServerAddress() {
    if(isset($_SERVER["SERVER_ADDR"]))
    return $_SERVER["SERVER_ADDR"];
    else {
    // Running CLI
    if(stristr(PHP_OS, 'WIN')) {
        //  Rather hacky way to handle windows servers
        exec('ipconfig /all', $catch);
        foreach($catch as $line) {
        if(eregi('IP Address', $line)) {
            // Have seen exec return "multi-line" content, so another hack.
            if(count($lineCount = split(':', $line)) == 1) {
            list($t, $ip) = split(':', $line);
            $ip = trim($ip);
            } else {
            $parts = explode('IP Address', $line);
            $parts = explode('Subnet Mask', $parts[1]);
            $parts = explode(': ', $parts[0]);
            $ip = trim($parts[1]);
            }
            if(ip2long($ip > 0)) {
            echo 'IP is '.$ip."\n";
            return $ip;
            } else
            ; // TODO: Handle this failure condition.
        }
        }
    } else {
        $ifconfig = shell_exec('/sbin/ifconfig eth0');
        preg_match('/addr:([\d\.]+)/', $ifconfig, $match);
        return $match[1];
    }
    }
}

回答by antik

If all else fails, you could always execipconfig or ifconfig, depending on your platform, and parse the result.

如果所有其他方法都失败了,您可以始终执行ipconfig 或 ifconfig,具体取决于您的平台,并解析结果。