How to create a domain checker using PHP?

  • Post last modified:January 18, 2022
  • Post category:How To
  • Post comments:0 Comments

Hi there, you might have seen a lot of websites where you can quickly look up if a domain name is available or not. Ever wondered how it works under the hood? or how can you build your own domain checker tool? Well, this is exactly what we’re going to do today. We’ll discuss how a domain checker works, and how you can create your own domain checker.

domain checker
Photo by Markus Winkler on Pexels.com

How domain checker tools work?

First, we need to understand how a domain checker tool works in order to create our own one. There are several ways a domain name checker can perform a query. But the most popular ways are checking Domain Name search also known as DNS and checking for WHOIS records.

But a domain availability checker is not limited to choosing just one method. Many of them like to combine both of them to deliver the best user experience. They tend to be more accurate than the one that is using just a single method.

Check domain availability using PHP function

There are a few built-in PHP functions available that can help you to determine if a domain name is available or not. In this article, we’re going to use one of them. The function is called gethostbyname, Basically, this function returns the IP address for a domain. So, if we call gethostbyname(“google.com”), this will return 142.250.190.14, which is the IP address of Google. But here’s a tricky part. It cannot return the IP address of a non existed domain, so it will return the domain itself if there is no IP address found for the domain. For example, if we call gethostbyname(“nonexisteddomain.io”), it will return nonexisteddomain.io

and we will take advantage of the fact and will build a domain availability checker. Just copy the code below and paste it on a PHP file and we’re good to go.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Domain checker</title>
	<link rel="stylesheet" href="./style.css">
</head>
<body>
	<div id="wrapper">		
		<form method="POST">
			<input type="text" name="domain" <?php if(isset($_POST['domain'])) echo 'value="'.$_POST['domain'].'"' ?>>
			<button type="submit">Check</button>
		</form>
		<div class="result">
			<?php 
				if(isset($_POST['domain'])){
					if ( gethostbyname($_POST['domain']) == $_POST['domain'] )
					    echo "<p class='success'>Congatulations! {$_POST['domain']} is available</p>";
					else
						echo "<p class='failur'>Sorry, {$_POST['domain']} is not available</p>";
				}
			?>
		</div>
	</div>
</body>
</html>

And if you prefer some styles, create a file called style.css in the same directory and paste the code below.

*{
	margin: 0;
	padding: 0;
}
body{
	background: #f4f9ff;
}
#wrapper{
	width: 100%;
	max-width: 460px;
	background: #fff;
		margin: 20px auto;
	padding: 20px;
	border-bottom: 1px solid #e7e7e7;
}
input{
	width: 100%;
	padding: 10px 0;
	margin-bottom: 20px;
}
button{
	border: none;
	background: #e7e7e7;
	padding: 10px 20px;
}
.result{
	text-align: center;
	font-size: 22px;
	margin-top: 20px;
}
.success{
	color: green;
}
.failur{
	color: red;
}

Please note: As it’s just a one-page script, I have not provided any demo link like I did for the YouTube Video Downloader Using PHP. But you can report any issues using the comment section below.

Check domain availability using Third party API or WHOIS Records

If you’re not happy with the PHP functional approach, you may try using some third-party API to fetch WHOIS records for a domain. This way, you will find exact WHOIS records for a domain, including if the domain is available or not. In this article, we’re going to use Whois Lookup API endpoints. I love this API because it provides a free tier of 500 monthly calls for absolutely free! It’s pretty straightforward, just use the code below to create your checker within seconds.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Domain checker</title>
	<link rel="stylesheet" href="./style.css">
</head>
<body>
	<div id="wrapper">		
		<form method="POST">
			<input type="text" name="domain" <?php if(isset($_POST['domain'])) echo 'value="'.$_POST['domain'].'"' ?>>
			<button type="submit">Check</button>
		</form>
		<div class="result">
			<?php 
				if(isset($_POST['domain'])){
					$domain = $_POST['domain'];
					$rapidapi_key = 'YOUR_RAPID_API_KEY_HERE';

					$curl = curl_init();

					curl_setopt_array($curl, [
						CURLOPT_URL => "https://zozor54-whois-lookup-v1.p.rapidapi.com/?domain=".$domain."&format=json",
						CURLOPT_RETURNTRANSFER => true,
						CURLOPT_FOLLOWLOCATION => true,
						CURLOPT_ENCODING => "",
						CURLOPT_MAXREDIRS => 10,
						CURLOPT_TIMEOUT => 30,
						CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
						CURLOPT_CUSTOMREQUEST => "GET",
						CURLOPT_HTTPHEADER => [
							"x-rapidapi-host: zozor54-whois-lookup-v1.p.rapidapi.com",
							"x-rapidapi-key: ".$rapidapi_key
						],
					]);

					$response = curl_exec($curl);
					$err = curl_error($curl);

					curl_close($curl);

					$response =json_decode($response);
					if ( !$err && $response->registered == false)
					    echo "<p class='success'>Congatulations! {$_POST['domain']} is available</p>";
					else
						echo "<p class='failur'>Sorry, {$_POST['domain']} is not available</p>";
				}
			?>
		</div>
	</div>
</body>
</html>

Please make sure you’ve subscribed to the rapid API in order to get a rapid API key. Also, don’t forget to replace the YOUR_RAPID_API_KEY_HERE with your actual rapid API key before testing this file. Once again, create the style.css file mentioned above in the same directory if you love to add some basic styles to your checker.

Optimize Your Checker – DNS and WHOIS records combined

Just looking for the IP address for the domain using the PHP function might not always give you accurate results. But each API calls to check WHOIS records will cost you. That’s why it will be great if we can combine both of them to provide absolute accurate results with the lowest API calls possible. That’s exactly what I did to optimize the script and you can get the optimized version of the script from here

Khokon M.

Full Stack Web Developer, Content Writer.

Leave a Reply