是否有可能找到使用PHP连接的网络接口客户端


Is it possible to find which network interface client is connected to with PHP?

我想找到客户端连接的网络接口。这可能吗?

如果使用Bash或其他脚本语言可能的话。我可以接受。

我在Freebsd上工作。

代码应该是这样的

function get_connected_interface(){
//...
}
echo get_connected_interface(); //should print network interface. for example em0

如果您使用的是Apache, PHP将$_SERVER["SERVER_ADDR"]设置为客户端连接到服务器的目的地 ip地址。

如果你正在使用nginx, lighttpd或其他web服务器,看看只有<?php phpinfo(); ?>的页面的结果,知道如何获得目的地的 ip地址(你的服务器的ip地址之一)

在任何情况下,您都可以使用此PHP脚本启动ifconfig并搜索具有此ip地址的接口。

我想说:

function get_connected_interface() {
  static $interfaces=array();
  if (!count($interfaces)) {
    $curif="";
    // launch ifconfig and parse its result (inet/inet6)
    // but only at first function call
    exec("ifconfig",$out);
    foreach($out as $line) {
      if (preg_match("#^([a-z0-9'.]*): #",$line,$mat)) {
    $curif=$mat[1];
      }
      if (preg_match("#inet ([0-9'.]*) #",$line,$mat)) {
    $interfaces[$mat[1]]=$curif;
      }
      if (preg_match("#inet6 ([0-9a-fA-F:]*) #",$line,$mat)) {
    $interfaces[$mat[1]]=$curif;
      }
    }
  }
  return $interfaces[$_SERVER["SERVER_ADDR"]];
}