PHP显示服务器IP地址 [英] PHP display server IP Address

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

问题描述

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

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);
}

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

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?

推荐答案

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

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.

参见 http://www.php.net/manual/en/reserved.variables.server。 php

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

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