featured-image

Domain Name Search Script In PHP |Short And Easy|

If you’ve ever wondered how those domain checker tools work on various websites, you’re in the right place. At KM Hosting, we believe in empowering our users with knowledge that helps them understand the technical aspects of web development. Today, we’ll dive into the mechanics behind domain name search scripts and show you how to create your own using PHP. This guide will walk you through the process step by step in an easy-to-understand way.

What is a Domain Name Search Script?

A domain name search script is a tool that allows users to check the availability of a domain name. When someone enters a domain name into the tool, it checks if that domain is already taken or if it’s available for registration. These scripts are essential for anyone looking to set up a website, as securing the right domain name is one of the first steps in establishing an online presence.

How Domain Name Search Tools Work

Before we dive into the coding part, it’s important to understand how these tools work. There are primarily two methods used to check domain availability:

1. DNS Lookup (Domain Name System): This method checks if the domain name is associated with an IP address. If it is, the domain is likely taken.

2. WHOIS Lookup: This method checks the WHOIS database for information about the domain. WHOIS records contain details about the domain’s registration status, ownership, and more.

Most domain name search tools combine these two methods to provide accurate results. While DNS lookup is fast, WHOIS lookup is more reliable but may take longer to execute.

Building a Basic Domain Name Search Script in PHP

Let’s start with a simple script that uses PHP’s built-in `gethostbyname()` function to perform a DNS lookup. This method is straightforward and easy to implement.

Step 1: Set Up Your PHP Environment

Before writing the code, ensure you have a working PHP environment. You can use a local server like XAMPP or WAMP, or host your script on a web server with PHP support.

For a professional development the visual Studio’s environment can be the best for run php.

image of visual studio and "domain name search script in php"

Step 2: Write the HTML and PHP Code

Below is a simple script that takes a domain name input from a user, checks its availability using `gethostbyname()`, and then displays the result.

PHP

				
					<!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" placeholder="Enter domain name" <?php if(isset($_POST['domain'])) echo 'value="'.htmlspecialchars($_POST['domain']).'"'; ?>>

			<button type="submit">Check</button>

		</form>

		<div class="result">

			<?php 

				if(isset($_POST['domain'])){

					$domain = $_POST['domain'];

					$ip = gethostbyname($domain);


					if ($ip == $domain) {

					    echo "<p class='success'>Congratulations! <strong>".htmlspecialchars($domain)."</strong> is available</p>";

					} else {

					    echo "<p class='failure'>Sorry, <strong>".htmlspecialchars($domain)."</strong> is not available</p>";

					}

				}

			?>

		</div>

	</div>

</body>

</html>
				
			

Step 3: Add Some Basic CSS

To make your domain checker look a bit more polished, you can add some basic CSS:

CSS

				
					<style>{

margin: 0;

padding: 0;

box-sizing: border-box;

}

body {

background: #f4f9ff;

font-family: Arial, sans-serif;

}

#wrapper {

width: 100%;

max-width: 460px;

background: #fff;

margin: 20px auto;

padding: 20px;

border: 1px solid #e7e7e7;

border-radius: 8px;

box-shadow: 0 0 10px rgba(0,0,0,0.1);

}

input {

width: 100%;

padding: 10px;

margin-bottom: 20px;

border: 1px solid #ccc;

border-radius: 4px;

}

button {

border: none;

background: #28a745;

color: #fff;

padding: 10px 20px;

border-radius: 4px;

cursor: pointer;

}

.result {

text-align: center;

font-size: 18px;

margin-top: 20px;

}

.success {

color: green;

}

.failure {

color: red;

}</style>
				
			

How the Script Works

1. HTML Form: The user enters a domain name in the text field and submits the form.

2. PHP Script: The script captures the input and checks if the domain has an associated IP address using `gethostbyname()`.

3. Output: If the domain has an IP address, it means the domain is taken, otherwise, it’s available.

Expanding the Script with WHOIS Lookup

For a more robust solution, you can incorporate a WHOIS lookup using a third-party API. This method provides more detailed information about the domain’s registration status.

Here’s a brief example of how to integrate WHOIS lookup using an API:

PHP

				
					// The same HTML structure as before

<?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'>Congratulations! <strong>".htmlspecialchars($domain)."</strong> is available</p>";

} else {

    echo "<p class='failure'>Sorry, <strong>".htmlspecialchars($domain)."</strong> is not available</p>";

}

}

?>

				
			

Conclusion

Creating a domain name search script in PHP is an excellent way to understand the basics of web development and domain registration. By using simple PHP functions and integrating third-party APIs, you can build a functional tool that checks domain availability.

At KM Hosting, we encourage learning and development, and this guide is a step towards helping you build useful web tools. Whether you’re a beginner or an experienced developer, understanding how domain checkers work will give you a deeper insight into the web hosting world.

If you need any assistance or want to learn more about our services, feel free to reach out to us at KM Hosting!

Post Your Comment

Your email address will not be published. Required fields are marked *

© Copyright 2024 KM Hosting a Trademark of Khazimulile Holdings. All rights reserved