【问题标题】:How do I find my server's IP address in PHP(CLI)如何在 PHP(CLI) 中找到我的服务器的 IP 地址
【发布时间】:2010-12-21 08:08:44
【问题描述】:

除了显而易见的(localhost,127.0.0.1)外,PHP(命令行界面!)是否有一种机制可以发现运行脚本的计算机的 IP?

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

TIA

【问题讨论】:

    标签: ip-address php


    【解决方案1】:

    您可以使用gethostname获取主机名

    【讨论】:

    • 谢谢 - 这太棒了:我的解决方案是:getHostByName(getHostName());
    • 页面底部有一条评论
    • 请注意:此方法(和gethostbynamel())将始终返回映射到服务器本地主机文件中的任何 IP。在我的测试(Ubuntu 11.10)中,这意味着如果您要检查的主机名映射到 /etc/hosts 中的 127.0.0.1 anywhere,它将覆盖任何其他条目集。
    【解决方案2】:

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

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

    【讨论】:

    • @Besnik 如果您使用 php 版本 = 4.2.0,请使用:$hostname = php_uname('n');。以上适用于 php 版本 > 5.3.0
    • @aneesh +1 完美! php -r "echo gethostbyname(php_uname('n'));" 显示 192.168.178.20 ...我正在使用 php 5.3.8
    【解决方案3】:

    如果您使用的是 PHP

     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:~$
    

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

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

    【讨论】:

    • 谢谢 - 我喜欢你的 Linux/Unix 解决方案,但对于这个应用程序,我在 Windows 上运行......(是的,我知道...... Windows 上的 PHP CLI?!)。很高兴知道网络服务 - 当我需要“防火墙 IP 外”时,我必须记住它:)。
    • whatismyip.org 现在已经死了。更好whatismyhostname.com/raw/hostname这个例子
    【解决方案4】:

    我知道这是一个相当古老的问题,但似乎没有明确的答案(尽可能多的答案。)我需要确定这个值,无论是在 *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];
        }
        }
    }
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 如果我没有运行 5.3,我会退回到这样的操作系统级解决方案。谢谢!
      • 不确定有多可靠,但您可以跳过 exec() 并使用 php_uname('n') 拉取主机名以传递给 getHostByName()
      【解决方案6】:

      $ip = file_get_contents('http://icanhazip.com/');

      回声 $ip;

      【讨论】:

      • 这与 10 年前 this answer 提供的指导基本相同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 2012-02-01
      • 2011-08-13
      • 2017-06-20
      相关资源
      最近更新 更多