PHP how to get local IP of system

2020-01-24 11:31发布

I need to get local IP of computer like 192.*.... Is this possible with 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.

15条回答
可以哭但决不认输i
2楼-- · 2020-01-24 11:42

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

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',
)
查看更多
家丑人穷心不美
3楼-- · 2020-01-24 11:42

I fiddled with this question for a server-side php (running from Linux terminal)

I exploded 'ifconfig' and trimmed it down to the IP address.

Here it is:

$interface_to_detect = 'wlan0';
echo explode(' ',explode(':',explode('inet addr',explode($interface_to_detect,trim(`ifconfig`))[1])[1])[1])[0];

And of course change 'wlan0' to your desired network device.

My output is:

192.168.1.5
查看更多
ゆ 、 Hurt°
4楼-- · 2020-01-24 11:49

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.

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.

查看更多
爷的心禁止访问
5楼-- · 2020-01-24 11:52
$_SERVER['SERVER_ADDR']
查看更多
我只想做你的唯一
6楼-- · 2020-01-24 11:53

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

$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_connect doesn'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.

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

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

查看更多
SAY GOODBYE
7楼-- · 2020-01-24 11:53

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

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

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

?>
查看更多
登录 后发表回答