This is probably simple,
How do you get the server IP with PHP?
$_SERVER['SERVER_ADDR'] only gives the LAN ip
Moderator: General Moderators
It entirely depends on how you have your server setup.heiatufte wrote:Hi,
This is probably simple,
How do you get the server IP with PHP?
$_SERVER['SERVER_ADDR'] only gives the LAN ip
Code: Select all
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, in addition to the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80
Listen 24.24.24.24:80Code: Select all
$device = 'eth0';
$expression = '/^'.$device.'.*?inet addr:([\d\.]+)/ism';
preg_match($expression, `ifconfig $device`, $matches);
echo $matches[1];Code: Select all
d11wtq@d11wtq d11wtq $ php test.php
212.69.XX.XXXCode: Select all
//This could potentially be very insecure
//Fetches the IP address of eth0 or any given device
// returns false on failure
function get_server_ip($device='eth0')
{
if (preg_match('/[^\w-]/', $device)) return false; //For security
$expression = '/^'.$device.'.*?inet addr:([\d\.]+)/ism';
preg_match($expression, `/sbin/ifconfig $device`, $matches);
if (!empty($matches[1])) return $matches[1];
else return false;
}Code: Select all
escapeshellarg($device)Handy to knowjshpro2 wrote:thanks d11 that worked well,
the secure way to do it wasCode: Select all
escapeshellarg($device)
I really wouldn't want to be help responsible if someone on a shared server started fiddling with ifconfig... there's the potential there to change the both the MAC and the IP of the interface.... that's more down to the security of the *nix machine itself granted but all the same it never hurts to validate user (or developer) inputjshpro2 wrote: it doesn't really matter what you pass to ifconfig to my knowledge, so long as they don't "inject" any other commands (just like anything else that uses the shell), but then again this function is meant to be used internally from the application anyways.. unless you expect your developers to be rooting your box, it shouldn't even matter that much![]()
If you have a domain name you could always try using the function gethostbyname() with your fully qualified domain name as the inputheiatufte wrote: As for d11's suggestion: yeah it's a linux box (FC3). I've managed to setup apache, php and mysql on it but other than that I don't have much experience with linux (unfortunately; I'm learning Wink)
Anyway, your code doesn't work with me, it returns my LAN IP 192.168.x.x !
All computers are connected to a router, which is connected to the ADSL box, a not so uncommon setup. But I still need my external IP!
For the record, I have also access to a server located outside my LAN. Perhaps I could use that server to create a page which returns the remote IP, and then use MY server to fetch it from there? That would perhaps not be the best, but a possible, solution...