如何使用PHP获取服务器的本地IP地址? [英] How do I get the local IP address of the server using PHP?

查看:1845
本文介绍了如何使用PHP获取服务器的本地IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发仅在企业的本地网络上运行的PHP应用程序.该应用程序将使用自定义安装程序安装到服务器上,就像我们使用Stunnix Advanced Webserver所做的一样.

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.

作为使应用程序更易于使用的一部分,我计划显示服务器的本地IP ,以便网络中的其他计算机通过键入以下内容即可轻松访问该应用程序该IP在他们的地址栏中.

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.

问题是我无法获取服务器的本地IP .

The problem is I cannot get the LOCAL IP of the server.

我尝试过

SERVER_NAME (仅显示127.0.0.1)

SERVER_NAME ( Displays just 127.0.0.1 )

REMOTE_ADDR (显示客户端外部IP)

REMOTE_ADDR ( Displays client external IP )

SERVER_ADDR 显示正确的IP,但只有当我从使用该IP的客户端从客户端访问该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.

我只想显示通过http://localhost本身直接从服务器访问的服务器的本地IP .

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.

编辑

我正在开发一种跨平台的PHP服务器应用程序.一键安装程序将Apache,PHP安装程序和SQlite数据库与我们的PHP应用程序捆绑在一起,以使其尽可能地友好.任何人都可以将其部署在运行 Windows,Mac或Linux 的计算机上.安装后,当用户在服务器上打开应用程序时,他会看到 ip地址[local ip] 和可用于连接到该服务器的端口.该应用程序将仅在本地网络上运行,而不能在Internet上运行.

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.

它应该显示本地IP,就像名为Air Droid的Android应用一样. 屏幕截图: https://lh3.ggpht.com/PmLopRm_LwBnWb_BWBWQbbWcByBWbWcBdWbcBdWcBdBcBwBwBwBwBwBwByBwBwBwBwBwBwBwBwBwBwBwBwBwBwBwBwBwBwBwBJB8B8B9B8B9B8B8B9B8B9B9B9B6B6B1B

It should show the local IP just like the Android app called Air Droid does. Screenshot : https://lh3.ggpht.com/PmLopRm-Lj9WTnzm2MBI-bTbCLopAyqtd4C_4nvyDwLg8X0QwDbBbEREdWGHG5xku4s

推荐答案

此处出现的问题是这不是静态信息.任何给定的计算机都可以具有与多个网卡关联的多个IP地址.其中一些/全部/没有可能提供网络服务.对于您所问的操作系统问题,可能没有唯一的答案.

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.

如果您的服务器具有简单的单个IP地址配置,那么您最好对它进行硬编码-这是迄今为止最简单的选择.如果要动态确定它,以下是一些希望对您有用的信息:

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']包含在浏览器的地址栏中键入的用于访问页面的地址.如果通过在浏览器中键入(例如)http://192.168.0.1/index.php来访问页面,则$_SERVER['HTTP_HOST']将为192.168.0.1.
  • 如果您使用DNS名称访问该页面,则gethostbyname($_SERVER['HTTP_HOST']);会将DNS名称转换为IP地址.
  • $_SERVER['SERVER_NAME']包含已在Web服务器配置中配置为服务器名称的名称.如果要使用此功能,则最好在PHP中对其进行硬编码.
  • $_SERVER['SERVER_ADDR']将包含服务器的主IP地址的操作系统.这可能是(也可能不是)用于访问该页面的IP地址,并且可能是(也可能不是)绑定了Web服务器的IP地址.它在很大程度上取决于操作系统和服务器配置.如果服务器只有一个IP地址,那么可能是一个安全的选择,尽管在某些情况下该地址可能包含127.0.0.1.
  • $_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.
  • If you used a DNS name to access the page, 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.

短处在于,没有一种100%可靠的方法来确定Web服务器的保证工作IP地址,而无需检查为生成正在创建的页面而发送到Web服务器的信息,正如您所说的totally defeats its purpose.

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.

这篇关于如何使用PHP获取服务器的本地IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆