Build Domain WHOIS Lookup System with PHP

WHOIS (pronounced as “Who is”) is a query and response protocol that generally used to find the details of internet resources such as a domain name, an IP address block or an autonomous system and other important information. The WHOIS protocol server database stores and returns information in human readable format. The information is about domain’s current registrar, registrant information, expiration date etc.

So if you’re looking for building your own WHOIS Lookup system, then here at right place. In our previous tutorial you have learned how to create your own reusable CAPTCHA with PHP. In this tutorial you will learn how to build your own Domain WHOIS Lookup System with PHP. This WHOIS PHP script will support all type of domain from all countries all over the world.

Also, read:

We will cover this tutorial step by step with live example to display domain WHOIS details.

As we will cover this tutorial with live example to create Domain WHOIS Lookup Script with PHP, so the major files for this example is following.


  • index.php
  • functions.php

Step1: Design Whois Lookup HTML Form
First we will create file index.php and design HTML form with a input to enter domain name and a search button to get domain WHOIS information on form submit.

<div class="container">	
	<label class="text-info">
	<?php if($message) { ?>
		<span class="text-danger"><strong><?php echo $message; ?></strong></span>
	<?php } ?>	
	</label>					
	<div class="row">					
		<form name="form" class="form" 
action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">					
			<div class="col-md-6">				
				<div class="form-group">							
					<input type="text" 
name="domain"
 id="domain" class="form-control" value="<?php echo $domain; ?>"
 placeholder="Enter domain name">
				</div>	
			</div>
			<div class="col-md-4">								
				<div class="form-group">					
					<button type="submit" 
class="btn btn-info btn-md"><i class="fa fa-search"></i> WHOIS</button>					
				</div>	
			</div>					
		</form>				
	</div>	
</div>

Step2: Display Domain WHOIS Information
When a domain name entered and form submitted, we will get domain name without http or www and pass to function validateDomain() to check for valid domain name. Then we will call function lookUpDomain() to get domain WHOIS information and store into a variable $result to display it.

if(isset($_POST['domain'])){ 
	$domain = $_POST['domain'];	
	$domain = trim($domain);
	if(substr(strtolower($domain), 0, 7) == "http://") $domain = substr($domain, 7);
	if(substr(strtolower($domain), 0, 8) == "https://") $domain = substr($domain, 8);
	if(substr(strtolower($domain), 0, 4) == "www.") $domain = substr($domain, 4);
	if(validateDomain($domain)) {
		$result = lookUpDomain($domain);
	} else {
		$message = "Invalid Input!";
	}
}

We will display domain WHOIS information using result varible.

<?php
if($result) {
	echo "<pre>\n" . $result . "\n</pre>\n";
}
?>

Step3: Get Domain WHOIS Information
We will create a function lookUpDomain() and check for domain WHOIS server exist using $whoIsServers array in which have stored WHOIS server name. If domain WHOIS server is exist then we call function getWhoisServerDetails() to make request to WHOIS server to get domain details.

function lookUpDomain($domain){
	global $whoIsServers;
	$domainParts = explode(".", $domain);
	$tld = strtolower(array_pop($domainParts));
	$whoIsServer = $whoIsServers[$tld];
	if(!$whoIsServer) {
		return "Error: No appropriate Whois server found for $domain domain!";
	}
	$whoIsResult = getWhoisServerDetails($whoIsServer, $domain);
	if(!$whoIsResult) {
		return "Error: No results retrieved from $whoIsServer server for $domain domain!";
	} else {
		while(strpos($whoIsResult, "Whois Server:") !== FALSE){
			preg_match("/Whois Server: (.*)/", $whoIsResult, $matches);
			$secondary = $matches[1];
			if($secondary) {
				$whoIsResult = getWhoisServerDetails($secondary, $domain);
				$whoIsServer = $secondary;
			}
		}
	}
	return "$domain domain lookup results from $whoIsServer server:\n\n" . $whoIsResult;
}

Step4: Make Request to Domain WHOIS SERVER
In functions.php file, we will create function getWhoisServerDetails() to make request to WHOIS server and get WHOIS information.


function getWhoisServerDetails($whoIsServer, $domain) {
	$port = 43;
	$timeout = 10;
	$whoIsInfo = @fsockopen($whoIsServer, $port, $errno, $errstr, $timeout) or 
die("Socket Error " . $errno . " - " . $errstr);	
	fputs($whoIsInfo, $domain . "\r\n");
	$output = "";
	while(!feof($whoIsInfo)){
		$output .= fgets($whoIsInfo);
	}
	fclose($whoIsInfo);
	$whosIsResults = "";
	if((strpos(strtolower($output), "error") === FALSE) 
&& (strpos(strtolower($output), "not allocated") === FALSE)) {
		$whoIsRecords = explode("\n", $output);
		foreach($whoIsRecords as $whoIsRecord) {
			$whoIsRecord = trim($whoIsRecord);
			if(($whoIsRecord != '') && ($whoIsRecord{0} != '#') && ($whoIsRecord{0} != '%')) {
				$whosIsResults .= $whoIsRecord."\n";
			}
		}
	}
	return $whosIsResults;
}

You may also like:

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download

8 thoughts on “Build Domain WHOIS Lookup System with PHP

  1. Can you create a PHP script like whois.com, where the search result is displayed using URL, also, store the details in database and links are listed automatically in the sitemap, and if anyone searches directly by replacing the URL, then it also shows search result of WHOIS

  2. How do is filter the raw data? I want to display domain information and registration separately

Comments are closed.