Baumeister Mediasoft Engineering BME - Baumeister Mediasoft Engineering  PHP Application: DNS (Domain Name System) Lookups - English Version PHP-Anwendung: Domainnamenssystem-Abfragen - Deutsche Version
Baumeister Mediasoft Engineering
Bartels AutoEngineerCarLogServicesResourcesCompany ProfileContact Us
Baumeister Mediasoft Engineering » Resources » Web Development » PHP » Applications » DNS Lookup
PHP Applications

PHP Application: DNS (Domain Name System) Lookups

PHP - PHP Hypertext Preprocessor

IP Address Lookup

The gethostbyname() PHP function can be used to look up the IP address for a given domain name.

string gethostbyname(string hostname)

The gethostbyname() function expects a host or domain name string parameter without the http:// protocol part (e.g., www.bme.ie) and returns the IP address for the given hostname or the hostname itself if the lookup failed.

Domain Name Lookup

The gethostbyaddr() PHP function can be used to look up the host (domain) name for a given IP address.

string gethostbyaddr(string ipaddress)

The gethostbyaddr() function expects a IP address string parameter (e.g., 207.46.249.252) and returns the host or domain name for the given ipaddress or the ipaddress itself if the lookup failed.

Example

The following PHP and HTML code displays a HTML page with a form for IP address and domain name lookups:

<?php
// dnslookupip.php - DNS/IP Address Lookup
// Copyright (c) 2003-2006 by Manfred Baumeister (www.bme.ie)

// Page title
$pagetitle        'BME Resources - PHP Application: DNS Lookup';

// Prompts
$prompt_ip        'IP Address';
$prompt_dn        'Domain Name';

// Messages
$lookupfail        '<span style="color:red;">* lookup failed *</span>';

// Get submitted host/domain name
$dn    = isset($_REQUEST['dn']) ? $_REQUEST['dn'] : '';
if (
$dn == $prompt_dn)
{
    
$dn '';
}

// Get submitted ip address
$ip    = isset($_REQUEST['ip']) ? $_REQUEST['ip'] : '';
if (
$ip == $prompt_ip)
{
    
$ip '';
}

// Check if host/domain name specified
if ($dn)
{
    
// Domain name specified; IP address lookup request
    
if ($dn == 'me')
    {
        
$ip $_SERVER['REMOTE_ADDR'];
    }
    else
    {
        
// Lookup IP address by domain/host name
        
$ip = @gethostbyname($dn);
        if (
$ip == $dn)
        {
            
$ip $lookupfail;
        }
    }
    
$message $prompt_dn.' '.$dn.' :: '.$prompt_ip.' '.$ip;
}
// Check if IP address specified
else if ($ip)
{
    
// Lookup domain/host name by IP address    
    
$dn = @gethostbyaddr($ip);
    
// Check lookup
    
if ($dn == $ip)
    {
        
// IP address invalid or domain name not found
        
$dn $lookupfail;
    }
    
$message $prompt_ip.' '.$ip.' :: '.$prompt_dn.' '.$dn;
}
else
{
    
$message $prompt_dn.' '.$_SERVER['HTTP_HOST']
        .
' :: '.$prompt_ip.' '.$_SERVER['SERVER_ADDR'];
}
?>
<html>
<head>
<title><?php echo $pagetitle;?></title>
</head>
<body style="background-color:#cfcfcf;font-family:Arial;sans-serif;font-size:12px;">
<h3 style="font-size:13px;margin-bottom:0px;"><?php echo $pagetitle;?></h3>
<hr />
<p style="margin-top:4px;margin-bottom:4px;font-size:12px;">
<?php echo $message;?>
</p>
<form style="margin-top:4px;margin-bottom:4px;">
<input style="font-size:12px;" type="text" name="dn"
    value="<?php echo $prompt_dn;?>" size="30" />
<input style="font-size:12px;" type="text" name="ip" 
    value="<?php echo $prompt_ip;?>" size="15" /> 
<input style="font-size:12px;" type="submit" value="Lookup" />
</form>
<hr />
<p style="margin:0px;font-size:9px;color:#666666;">
Copyright &#169; 2003-<?php echo date('Y');?>
 by <a href="http://www.bme.ie" target="_top">Manfred Baumeister</a>
</p>
</body>
</html>

The following displays the HTML page from above using an <iframe> tag:

Click here to open the above page in its own window.

Baumeister Mediasoft Engineering » Resources » Web Development » PHP » Applications » DNS Lookup

PHP Application: DNS (Domain Name System) Lookups • © 2018 Manfred Baumeister • Updated: 02 October 2010, 23:36 [UTC]

Baumeister Mediasoft Engineering, Clontarf, Dublin 3, D03 HA22, Ireland
© 2018 Manfred Baumeister

PHP Application: DNS (Domain Name System) Lookups - English Version PHP-Anwendung: Domainnamenssystem-Abfragen - Deutsche Version