I am developing a PHP application that will be run only on the local network of a business. The application will be installed to the server using a custom installer like thi
The problem that you have here is that this is not a static piece of information. Any given computer can have multiple IP addresses associated with multiple network cards. Some/all/none of those may have the web service available on them. There may not be a single answer to the question you are asking of the OS.
If your server has a simple, single IP address configuration then you would probably be best to hard-code this - it is by far and away the simplest option. If you want to determine it dynamically, here are a few bits of information which you will hopefully find useful:
$_SERVER['HTTP_HOST']
contains the address that was typed into the address bar of the browser in order to access the page. If you access the page by typing (for example) http://192.168.0.1/index.php
into the browser, $_SERVER['HTTP_HOST']
will be 192.168.0.1
.gethostbyname($_SERVER['HTTP_HOST']);
will turn that DNS name into an IP address.$_SERVER['SERVER_NAME']
contains the name that has been configured in the web server configuration as it's server name. If you going to use this, you might as well just hard code it in PHP.$_SERVER['SERVER_ADDR']
will contain the operating system of the server's primary IP address. This may or may not be the IP address that was used to access the page, and it may or may not be an IP address with the web server bound to it. It depends heavily on OS and server configuration. if the server has a single IP address, this is probably a safe bet, although there are situations where this may contain 127.0.0.1
.The long of the short of it is that there is no 100% reliable way to determine a guaranteed working IP address of the web server, without examining information that was send to the web server in order to generate the page you are creating, which as you say totally defeats its purpose
.
Solutions:
ifconfig -l
or ipconfig -a
output to determine IP addresses of the machine's network interfaces, exclude loopback interfaces. In the best case you'll receive one IP address - and that'll be the address you need, adding $_SERVER['SERVER_PORT']
. If you'll get several IP addresses - you can try to query them all to check if they answer a HTTP requests on the $_SERVER['SERVER_PORT']
. Also you could put a /ping.php
file with some kind of echo "my application";
to determine your IP address if the machine runs several HTTP servers on different IP addresses.Try :
<?php gethostbyname($_SERVER['SERVER_NAME']);
EDIT :
A bit hacky, but it seems to work :
<?php
$str = file_get_contents("http://ip6.me/");
$pattern = "#\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b#";
preg_match($pattern, $str, $matches);
print_r($matches[0]);
But this rely on a third website..
You can see all the available information with the command:
print_r($_SERVER, 1);
If you want to get Lan IP Adress Like 192.168.x.x
You can use the following code:
I'm using this and fulfills my requirement perfectly. (only tested in windows machine)
function getLocalIP(){
exec("ipconfig /all", $output);
foreach($output as $line){
if (preg_match("/(.*)IPv4 Address(.*)/", $line)){
$ip = $line;
$ip = str_replace("IPv4 Address. . . . . . . . . . . :","",$ip);
$ip = str_replace("(Preferred)","",$ip);
}
}
return $ip;
}
echo $ip = getLocalIP(); //This will return: 192.168.x.x (Your Local IP)
Hope this helps. =)
I am also pretty sure this isn't possible. Or if it is possible, the results won't be reliable.
A server may have multiple IP addresses. Also, the IP address used for HTTP on the server may not be the one used to reach the server from elsewhere (if there's a reverse proxy or NAT involved). The SERVER_ADDR variable shows the IP address that was used to call a PHP script; this information is passed from the web server, so it won't help for a PHP script that's running stand-alone (i.e. from a shell).
Let's look at this another way. What problem are you trying to solve? That is, why do you need the local IP of the server? Rather than getting help implementing a solution that won't work, let's look at the original issue and see if there's another solution that IS possible.
I should point out that depending on your operating system, there may be ways to get a list of IPs assigned to various interfaces by parsing the output of a shell command like ifconfig -a
or ip addr
. But I don't know your OS, so I can't suggest how that would work for you.
$h = popen("ip addr | awk '/inet/{print$2}'");
$ip_list = array();
while ($ip_list[] = fread($h, 15)) { }
pclose($h);
You could put more logic into the PHP to avoid using awk
in a pipe, but you get the idea.