Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve IP to Hostname using PHP

Tags:

php

How can I resolve an IP address to a hostname using PHP?

like image 997
johnl Avatar asked Oct 05 '09 09:10

johnl


People also ask

How do I resolve an IP address to a hostname?

Click the Windows Start button, then "All Programs" and "Accessories." Right-click on "Command Prompt" and choose "Run as Administrator." Type "nslookup %ipaddress%" in the black box that appears on the screen, substituting %ipaddress% with the IP address for which you want to find the hostname.

How do I resolve an IP address to a website?

To do this in Chrome, simply open up the DevTools, navigate to the Network tab and select the site's HTML doc. You should then see the IP address associated with that URL under Headers > General > Remote Address.

How do I find my localhost IP in PHP?

The simplest way to collect the visitor IP address in PHP is the REMOTE_ADDR. Pass the 'REMOTE_ADDR' in PHP $_SERVER variable. It will return the IP address of the visitor who is currently viewing the webpage.


2 Answers

You can use the gethostbyaddr() function.

$hostname = gethostbyaddr($ipAddress);
like image 58
Greg Avatar answered Oct 30 '22 01:10

Greg


Use gethostbyaddr()

$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);

echo $hostname;

You might find that this function can hang for some time when an address is not found - if this becomes a performance issue for you, check this manual comment which details a hand-coded method which supports a timeout.

like image 30
Paul Dixon Avatar answered Oct 30 '22 02:10

Paul Dixon