PHP显示服务器IP地址
我正在开发一个网站,我想做的一件事就是向用户显示我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
$_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.
用于服务器 IP 地址
$_SERVER['SERVER_ADDR'];
和服务器端口
$_SERVER['SERVER_PORT'];
for server ip address
$_SERVER['SERVER_ADDR'];
and for server port
$_SERVER['SERVER_PORT'];
如果您的 laravel 应用程序运行在内部服务器上,您可以使用以下命令获取服务器的外部地址:
If your laravel application is running on an internal server, you can use the following to get the external address of the server: