title: Need to find IP address of network interface on Ubuntu with PHP
body: <p>I need help finding the IP address of my computer when it is on a network.  I am building a kiosk type system that will be put into different locations and I need to be able to use the web browser to find the IP address of that computer on the local network.</p>
  
  <p>If I use <code>$_SERVER['SERVER_ADDR']</code> I get the IP address I am connecting to through the local browser (127.0.0.1) on that machine.  </p>
  
  <p>I can't call out and get the public IP because the units may be behind a router and I don't want the public IP address of the router.  </p>
  
  <p>I need to find the IP address of that box on the server (ex: 192.168.0.xxx)</p>
  
  <p>I do know that when I do a 'ip addr show' from the terminal I get </p>
  
  <pre><code>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN 
      link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
      inet 127.0.0.1/8 scope host lo
      inet6 ::1/128 scope host 
         valid_lft forever preferred_lft forever
  2: em1: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP qlen 1000
      link/ether 61:a6:4d:63:a2:80 brd ff:ff:ff:ff:ff:ff
      inet 192.168.0.211/24 brd 192.168.0.255 scope global em1
      inet6 fe80::62a4:4cff:fe64:a399/64 scope link 
         valid_lft forever preferred_lft forever
  </code></pre>
  
  <p>When I try:</p>
  
  <pre><code>$command="ip addr show";
  $localIP = exec ($command);
  </code></pre>
  
  <p><code>$localIP</code> comes out with "valid_lft forever preferred_lft forever" but none of the other information.  If I can get the whole thing into <code>$localIP</code> then I can filter out the inet IP address but it won't give me everything.</p>
  
  <p>Is there an easier way to do this or something I am missing when trying to do the "ip addr show" command?  Also, I am running under the user apache and can't run this as root for this application.</p>