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.
If I use $_SERVER['SERVER_ADDR']
I get the IP address I am connecting to through the local browser (127.0.0.1) on that machine.
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.
I need to find the IP address of that box on the server (ex: 192.168.0.xxx)
I do know that when I do a 'ip addr show' from the terminal I get
1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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
When I try:
$command="ip addr show";
$localIP = exec ($command);
$localIP
comes out with "valid_lft forever preferred_lft forever" but none of the other information. If I can get the whole thing into $localIP
then I can filter out the inet IP address but it won't give me everything.
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.
As documented for exec()
, only the LAST line of output from the exec'd command is returned from the function. To capture all of the output, you have to use the optional 2nd argument to the function:
$last_line = exec('ip addr show', $full_output);
^^^^^^^^^^^^
$full_output
will be an array of lines of output from the exec'd program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With