PHP display server IP Address
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 sh开发者_如何学运维ould say "Moria.student.rit.edu" for the Hostname, and the IP address. Any help?
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.
for server ip address $_SERVER['SERVER_ADDR'];
and for server port $_SERVER['SERVER_PORT'];
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');
精彩评论