php PHP如何获取系统的本地IP

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

PHP how to get local IP of system

phpip-address

提问by air

I need to get local IP of computer like 192.*.... Is this possible with PHP?

我需要获得像 192.*.. 这样的计算机的本地 IP。这可以用 PHP 实现吗?

I need IP address of system running the script, but I do not need the external IP, I need his local network card address.

我需要运行脚本的系统的IP地址,但我不需要外部IP,我需要他的本地网卡地址。

回答by andras.tim

From CLI

从命令行界面

PHP < 5.3.0

PHP < 5.3.0

$localIP = getHostByName(php_uname('n'));

$localIP = getHostByName(php_uname('n'));

PHP >= 5.3.0

PHP >= 5.3.0

$localIP = getHostByName(getHostName());

$localIP = getHostByName(getHostName());

回答by Sergey Eremin

$_SERVER['SERVER_ADDR']

回答by Codebeat

This is an old post, but get it with this:

这是一篇旧帖子,但可以通过以下方式获取:

function getLocalIp()
{ return gethostbyname(trim(`hostname`)); }

For example:

例如:

die( getLocalIp() );

Found it on another site, do not remove the trim command because otherwise you will get the computers name.

在另一个站点上找到它,不要删除修剪命令,否则您将获得计算机名称。

BACKTICKS (The special quotes): It works because PHP will attempt to run whatever it's between those "special quotes" (backticks) as a shell command and returns the resulting output.

反引号(特殊引号):它之所以有效,是因为 PHP 会尝试将这些“特殊引号”(反引号)之间的任何内容作为 shell 命令运行并返回结果输出。

gethostbyname(trim(`hostname`));

Is very similar (but much more efficient) than doing:

与以下操作非常相似(但效率更高):

$exec = exec("hostname"); //the "hostname" is a valid command in both windows and linux
$hostname = trim($exec); //remove any spaces before and after
$ip = gethostbyname($hostname); //resolves the hostname using local hosts resolver or DNS

回答by George Deac

try this (if your server is Linux):

试试这个(如果你的服务器是 Linux):

$command="/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print }'";
$localIP = exec ($command);
echo $localIP;

回答by Arnaud Le Blanc

The default external IP address of the local machine can be obtained like this:

本地机器的默认外部IP地址可以这样获取:

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_connect($sock, "8.8.8.8", 53);
socket_getsockname($sock, $name); // $name passed by reference

$localAddr = $name;

How it works:

这个怎么运作:

Since this is UDP, socket_connectdoesn't go out on the network. It only assigns a local address and port to the socket. This allows us to get the local address.

由于这是UDP,因此socket_connect不会在网络上出现。它只为套接字分配一个本地地址和端口。这允许我们获取本地地址。

This solution works without any network traffic, DNS, or command execution.

此解决方案无需任何网络流量、DNS 或命令执行即可工作。

It may not work if there is no route to 8.8.8.8(e.g. you have no internet connection).

如果没有通往的路线8.8.8.8(例如您没有互联网连接),它可能无法工作。

回答by Fosco

Depends what you mean by local:

取决于你所说的本地是什么意思:

If by local you mean the address of the server/system executing the PHP code, then there are still two avenues to discuss. If PHP is being run through a web server, then you can get the server address by reading $_SERVER['SERVER_ADDR']. If PHP is being run through a command line interface, then you would likely have to shell-execute ipconfig(Windows) / ifconfig(*nix) and grep out the address.

如果本地是指执行 PHP 代码的服务器/系统的地址,那么仍有两种途径可以讨论。如果 PHP 正在通过 Web 服务器运行,那么您可以通过阅读$_SERVER['SERVER_ADDR']. 如果 PHP 是通过命令行界面运行的,那么您可能需要 shell-execute ipconfig(Windows) / ifconfig(*nix) 并 grep 出地址。

If by local you mean the remote address of the website visitor, but not their external IP address (since you specifically said 192.*), then you are out of luck. The whole point of NAT routing is to hide that address. You cannot identify the local addresses of individual computers behind an IP address, but there are some tricks (user agent, possibly mac address) that can help differentiate if there are multiple computers accessing from the same IP.

如果本地是指网站访问者的远程地址,而不是他们的外部 IP 地址(因为您特别提到了 192.*),那么您就不走运了。NAT 路由的全部意义在于隐藏该地址。您无法识别 IP 地址后面的单个计算机的本地地址,但是有一些技巧(用户代理,可能是 mac 地址)可以帮助区分是否有多台计算机从同一 IP 访问。

回答by ern0

hostname(1) can tell the IP address: hostname --ip-address, or as man says, it's better to use hostname --all-ip-addresses

hostname(1) 可以告诉 IP 地址:hostname --ip-address,或者正如 man 所说,最好使用hostname --all-ip-addresses

回答by ab1965

You may try this as regular user in CLI on Linux host:

您可以在 Linux 主机上的 CLI 中以普通用户的身份尝试此操作:

function get_local_ipv4() {
  $out = split(PHP_EOL,shell_exec("/sbin/ifconfig"));
  $local_addrs = array();
  $ifname = 'unknown';
  foreach($out as $str) {
    $matches = array();
    if(preg_match('/^([a-z0-9]+)(:\d{1,2})?(\s)+Link/',$str,$matches)) {
      $ifname = $matches[1];
      if(strlen($matches[2])>0) {
        $ifname .= $matches[2];
      }
    } elseif(preg_match('/inet addr:((?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3})\s/',$str,$matches)) {
      $local_addrs[$ifname] = $matches[1];
    }
  }
  return $local_addrs;
}

$addrs = get_local_ipv4();
var_export($addrs);

Output:

输出:

array (
  'eth0' => '192.168.1.1',
  'eth0:0' => '192.168.2.1',
  'lo' => '127.0.0.1',
  'vboxnet0' => '192.168.56.1',
)

回答by Chinthaka Devinda

It is very simple and above answers are complicating things. Simply you can get both local and public ip addresses using this method.

这很简单,上面的答案使事情复杂化。只需使用此方法即可获取本地和公共 IP 地址。

   <?php 
$publicIP = file_get_contents("http://ipecho.net/plain");
echo $publicIP;

$localIp = gethostbyname(gethostname());
echo $localIp;

?>

回答by Pratik Boda

$localIP = gethostbyname(trim(exec("hostname")));

I tried in Windows pc and Its worked and also think that Will work on Linux to.

我在 Windows pc 上尝试过,它可以工作,也认为可以在 Linux 上工作。