Ethan Mick Ethan Mick - 11 months ago 64
PHP Question

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 "". Not quite what I am looking for. Rather, it should say "" 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.