Ethan Mick
Ethan Mick

Reputation: 9567

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 should say "Moria.student.rit.edu" for the Hostname, and the IP address. Any help?

Upvotes: 24

Views: 86102

Answers (3)

Ravi
Ravi

Reputation: 324

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

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

Upvotes: 1

jmurray
jmurray

Reputation: 17

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

Upvotes: -9

Matthew
Matthew

Reputation: 48284

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.

Upvotes: 55

Related Questions