PHP显示服务器IP地址

发布于 2024-10-07 15:37:35 字数 443 浏览 0 评论 0原文

我正在开发一个网站,我想做的一件事就是向用户显示我的 IP 地址。该网站是用 CodeIgniter 制作的,所以我想用 PHP 找到我的服务器 IP。 IP地址可能会改变(它是漫游者),所以我想动态地找到它,而不仅仅是硬编码它。我尝试了这个:

$data['hostname'] = NULL;
$data['ip'] = NULL;
$var = gethostname();
if ($var === FALSE) {
  $var = NULL;
} else {
  $data['hostname'] = $var;
  $data['ip']   = gethostbyname($var);
}

但是,我没有给我主机名和IP,而是得到:“Moria”和“127.0.1.1”。不完全是我想要的。相反,主机名和 IP 地址应该为“Moria.student.rit.edu”。有什么帮助吗?

I'm working on a website, and one of the things I would like to do is display MY IP address to users. The website is made with CodeIgniter, so I was looking to find my server IP with PHP. The IP address may change (it's a roamer), so I'd like to find it dynamically, not just hard code it. I tried this:

$data['hostname'] = NULL;
$data['ip'] = NULL;
$var = gethostname();
if ($var === FALSE) {
  $var = NULL;
} else {
  $data['hostname'] = $var;
  $data['ip']   = gethostbyname($var);
}

However, instead of giving me the Hostname and the IP, I got: "Moria" and "127.0.1.1". Not quite what I am looking for. Rather, it should say "Moria.student.rit.edu" for the Hostname, and the IP address. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃温暖了心ぐ 2024-10-14 15:37:35

尝试$_SERVER['SERVER_ADDR']。它将是服务器正在侦听的 IP 地址。您可以使用 DNS 函数(例如,gethostbyaddr())来获取主机名。

请参阅 http://www.php.net/manual/en/reserved。变量.server.php

Try $_SERVER['SERVER_ADDR']. It will be the IP address that the server is listening on. You can use DNS functions (e.g., gethostbyaddr()) to get the host name.

See http://www.php.net/manual/en/reserved.variables.server.php.

疯了 2024-10-14 15:37:35

用于服务器 IP 地址
$_SERVER['SERVER_ADDR'];

和服务器端口
$_SERVER['SERVER_PORT'];

for server ip address
$_SERVER['SERVER_ADDR'];

and for server port
$_SERVER['SERVER_PORT'];

木緿 2024-10-14 15:37:35

如果您的 laravel 应用程序运行在内部服务器上,您可以使用以下命令获取服务器的外部地址:

 $external_ip = exec('curl http://ipecho.net/plain; echo');

If your laravel application is running on an internal server, you can use the following to get the external address of the server:

 $external_ip = exec('curl http://ipecho.net/plain; echo');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文