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

查看:38
本文介绍了如何使用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 从客户端访问它时才会这样做.

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 本身直接从服务器访问时显示服务器的 LOCAL 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 地址 [本地 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.

它应该像名为 Air Droid 的 Android 应用程序一样显示本地 IP.截图: https://lh3.ggpht.com/PmLopRm-Lj9WTnzm2MBI-bTbCLopAyqtd4C_4nvyDwLg8X0QwDbBbEREdWGHG5xku4s

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 地址.其中一些/全部/没有一个可能具有可用的 Web 服务.您向操作系统提出的问题可能没有单一答案.

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 服务器以生成您正在创建的页面的信息,正如你所说的完全违背了它的目的.

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天全站免登陆