php 如何使用PHP获取服务器的本地IP地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9905650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the local IP address of the server using PHP?
提问by ajaybc
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.
我正在开发一个只能在企业的本地网络上运行的 PHP 应用程序。该应用程序将使用自定义安装程序安装到服务器,就像我们使用 Stunnix Advanced Webserver 制作的一样。
As part of making the application more user friendly I am planning to display the LOCAL IPof 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,以便网络中的其他计算机只需在地址栏中键入此 IP 即可非常轻松地访问应用程序。
The problem is I cannot get the LOCAL IPof the server.
问题是我无法获得服务器的本地 IP。
I have tried
我试过了
SERVER_NAME( Displays just 127.0.0.1 )
SERVER_NAME(仅显示 127.0.0.1)
REMOTE_ADDR( Displays client external IP )
REMOTE_ADDR(显示客户端外部 IP)
SERVER_ADDRdisplays the correct IP but it does so only if I access it from a client using the IP which totally defeats its purpose.
SERVER_ADDR显示正确的 IP,但只有当我使用完全违背其目的的 IP 从客户端访问它时才会这样做。
I just want to display the LOCAL IPof the server upon access directly from the server through http://localhost
itself.
我只想在直接从服务器通过自身访问时显示服务器的本地 IPhttp://localhost
。
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.
我正在开发一个跨平台的 PHP 服务器应用程序。我们将 Apache、PHP 安装程序和 SQlite 数据库作为一键安装程序与我们的 PHP 应用程序捆绑在一起,以使其尽可能用户友好。任何人都可以将其部署在运行Windows、Mac 或 Linux的计算机上。安装后,当用户在服务器上打开应用程序时,他可以看到ip 地址 [本地 ip]和可用于连接到此服务器的端口。该应用程序将仅在本地网络上运行,而不在 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
它应该像名为 Air Droid 的 Android 应用程序一样显示本地 IP。截图:https: //lh3.ggpht.com/PmLopRm-Lj9WTnzm2MBI-bTbCLopAyqtd4C_4nvyDwLg8X0QwDbBbEREDdWGHG5xku4s
回答by DaveRandom
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 地址。其中一些/全部/没有一个可能具有可用的 Web 服务。您对操作系统提出的问题可能没有单一的答案。
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:
如果您的服务器有一个简单的单一 IP 地址配置,那么您可能最好对其进行硬编码 - 这是迄今为止最简单的选择。如果你想动态地确定它,这里有一些你会发现有用的信息:
$_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 be192.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 probablya safe bet, although there are situations where this may contain127.0.0.1
.
$_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
.
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
.
总而言之,没有 100% 可靠的方法来确定 Web 服务器的保证工作 IP 地址,而无需检查发送到 Web 服务器以生成您正在创建的页面的信息,如你说totally defeats its purpose
。
回答by Ilya Sheershoff
Solutions:
解决方案:
- hardcode in php (upon installation one should edit this in your scripts)
- setup a variable in apache environment so that could be accessed through php (upon installation one should add this to apache's config)
- parse
ifconfig -l
oripconfig -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 ofecho "my application";
to determine your IP address if the machine runs several HTTP servers on different IP addresses.
- php 中的硬编码(安装时应该在脚本中编辑它)
- 在 apache 环境中设置一个变量,以便可以通过 php 访问(安装时应将其添加到 apache 的配置中)
- 解析
ifconfig -l
或ipconfig -a
输出以确定机器网络接口的 IP 地址,排除环回接口。在最好的情况下,您将收到一个 IP 地址 - 这将是您需要的地址,添加$_SERVER['SERVER_PORT']
. 如果您将获得多个 IP 地址 - 您可以尝试全部查询它们以检查它们是否在$_SERVER['SERVER_PORT']
. 如果机器在不同的 IP 地址上运行多个 HTTP 服务器,您也可以放置一个/ping.php
带有某种类型的文件echo "my application";
来确定您的 IP 地址。
回答by AlphaMale
If you want to get Lan IP AdressLike 192.168.x.x
You can use the following code:
如果你想获得Lan IP AdressLike192.168.x.x
你可以使用以下代码:
I'm using this and fulfills my requirement perfectly. (only tested in windows machine)
我正在使用它并完美满足我的要求。(仅在 windows 机器上测试过)
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. =)
希望这可以帮助。=)
回答by ghoti
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).
一个服务器可能有多个 IP 地址。此外,服务器上用于 HTTP 的 IP 地址可能不是用于从其他地方访问服务器的 IP 地址(如果涉及反向代理或 NAT)。SERVER_ADDR 变量显示用于调用 PHP 脚本的 IP 地址;此信息是从 Web 服务器传递的,因此它对独立运行的 PHP 脚本(即来自 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.
让我们换一种方式来看待这个问题。你想解决什么问题?也就是说,为什么需要服务器的本地IP?与其寻求帮助实施不起作用的解决方案,不如让我们看看原始问题,看看是否还有其他可行的解决方案。
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.
我应该指出,根据您的操作系统,可能有一些方法可以通过解析 shell 命令(如ifconfig -a
或 )的输出来获取分配给各种接口的 IP 列表ip addr
。但我不知道你的操作系统,所以我不能建议你如何工作。
$h = popen("ip addr | awk '/inet/{print}'");
$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.
您可以将更多逻辑放入 PHP 以避免awk
在管道中使用,但您明白了。
回答by Adrien Schuler
Try :
尝试 :
<?php gethostbyname($_SERVER['SERVER_NAME']);
<?php gethostbyname($_SERVER['SERVER_NAME']);
EDIT :
编辑 :
A bit hacky, but it seems to work :
有点hacky,但似乎有效:
<?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..
但这依赖于第三个网站..
回答by Schiavini
You can see all the available information with the command:
您可以使用以下命令查看所有可用信息:
print_r($_SERVER, 1);
回答by Dipesh
Try using "ifconfig en1 inet" Command.
尝试使用“ifconfig en1 inet”命令。
print_r(exec("ifconfig en1 inet"));
print_r(exec("ifconfig en1 inet"));
Output:- inet 192.168..netmask 0xffffff00 broadcast 192.168.*.255
输出:- inet 192.168。. 网络掩码 0xffffff00 广播 192.168.*.255