I need to run a php script on a linux box that will resolve and reutrn its own ip address - the ip address of the local host. I have seen a lot of posts were the result is just 127.0.0.0, but I need the linux box's ip address that is seen by the LAN (like 192.168.0.2).
Thank you.
David
Resolve localhost ip address
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Resolve localhost ip address
That would be $_SERVER["SERVER_ADDR"]. You should get familiar with phpinfo(). 
(#10850)
-
WebbieDave
- Forum Contributor
- Posts: 213
- Joined: Sun Jul 15, 2007 7:07 am
Re: Resolve localhost ip address
Is the script being invoked via a web server or command line? If the former, you can try $_SERVER['SERVER_ADDR'] but it may not always be available or yield useful results. Also, some web servers place its ip in $_SERVER['SERVER_NAME'] instead. If via command line, you can regex it from ifconfig. Keep in mind that an interface can have multiple ip addresses.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Resolve localhost ip address
If your script is not being called via a webserver, it will have to query the OS to find out the IP address. It's not something PHP would intrinsically know.
Re: Resolve localhost ip address
Thank you for your replies.
The sript would not be called via a webserver, just php script on a linux box (which does run apache). I have seen that C# has the ability to call an API that can get the server address, but I need to do it in php with a short script.
Is there no method I can call in php to query what the OS sees as the ip address? I see a suggestion for ifconfig and then use regular express to capture the ip address.
The sript would not be called via a webserver, just php script on a linux box (which does run apache). I have seen that C# has the ability to call an API that can get the server address, but I need to do it in php with a short script.
Is there no method I can call in php to query what the OS sees as the ip address? I see a suggestion for ifconfig and then use regular express to capture the ip address.
Re: Resolve localhost ip address
#!/bin/sh
IP_ADDR=`/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2
echo "SET VARIABLE IP_ADD $IP_ADDR"
exit 0;
IP_ADDR=`/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2
echo "SET VARIABLE IP_ADD $IP_ADDR"
exit 0;
Re: Resolve localhost ip address
A little fix:
OR by using iproute:
-->dghundt wrote:IP_ADDR=`/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2
Code: Select all
IP_ADDR=`/sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2| cut -d" " -f1Code: Select all
ip route show | grep src | awk '{print $9}'There are 10 types of people in this world, those who understand binary and those who don't