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 thing we made using Stunnix Advanced Webserver.
As part of making the application more user friendly I am planning to display the LOCAL IP of the server so that it is extremely easy for the other computers in the network to access the application by just typing this IP in their address bar.
The problem is I cannot get the LOCAL IP of the server.
I have tried
SERVER_NAME ( Displays just 127.0.0.1 )
REMOTE_ADDR ( Displays client external IP )
SERVER_ADDR displays the correct IP but it does so only if I access it from a client using the IP which totally defeats its purpose.
I just want to display the LOCAL IP of the server upon access directly from the server through http://localhost
itself.
I am somewhat sure that this isn't possible. But if it is, please help.
EDIT
I am developing a cross platform PHP server application kind of thing. We bundle Apache,PHP installers and a SQlite database as a one click installer alongside our PHP application to make it as user friendly as possible. Anyone can deploy it on their computer running Windows,Mac or Linux. After installing when the user opens the application on the server he can see the ip address [local ip] and port which can be used to connect to this server. The application will be run only on the local network and not on the internet.
It should show the local IP just like the Android app called Air Droid does. Screenshot : https://lh3.ggpht.com/PmLopRm-Lj9WTnzm2MBI-bTbCLopAyqtd4C_4nvyDwLg8X0QwDbBbEREdWGHG5xku4s
getHostByName() Function: It gets the IPv4 address corresponding to a given Internet host name. getHostName() Function: It gets the standard host name for the local machine.
First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.
getenv() function: The other way to get the IP address of the client is using the getenv() function. It is used to retrieve the value of an environment variable in PHP. To get the IP address of the user we need to pass the REMOTE_ADDR variable to the getenv() function. getenv("REMOTE_ADDR");
Usually, you can access the localhost of any computer through the loopback address 127.0. 0.1. By default, this IP address references a server running on the current device. In other words, when your computer requests the IP address 127.0.
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.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