Resolve localhost ip address

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dghundt
Forum Newbie
Posts: 3
Joined: Fri Jun 13, 2008 8:50 pm

Resolve localhost ip address

Post by dghundt »

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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Resolve localhost ip address

Post by Christopher »

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

Post by WebbieDave »

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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Resolve localhost ip address

Post by Ambush Commander »

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.
dghundt
Forum Newbie
Posts: 3
Joined: Fri Jun 13, 2008 8:50 pm

Re: Resolve localhost ip address

Post by dghundt »

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.
dghundt
Forum Newbie
Posts: 3
Joined: Fri Jun 13, 2008 8:50 pm

Re: Resolve localhost ip address

Post by dghundt »

#!/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;
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Resolve localhost ip address

Post by VladSun »

A little fix:
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" " -f1
OR by using iproute:

Code: 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
Post Reply