Build Domain WHOIS Lookup System using PHP and Ajax
WHOIS (pronounced as the phrase “who is”) is a query and response protocol that is widely used for querying databases that store the registered users or assignees of an Internet resource, such as a domain name, an IP address block or an autonomous system, but is also used for a wider range of other information. The protocol stores and delivers database content in a human-readable format.
In this tutorial you will learn how to build your own Domain WHOIS Lookup System with PHP and Ajax. This WHOIS PHP script will support all types of domains from all countries all over the world. This is a very simple example, you can just copy paste, and change.
Before started to build your own Domain WHOIS Lookup System with PHP, look files structure:
- build-domain-whois-lookup-system-using-php
- css
- style.css
- images
- templates
- header.php
- footer.php
- index.php
- lookup.php
- css
Step 1: Design Whois Lookup HTML Form
Create file named index.php and design HTML form with a input to enter domain name or IP Address and a search button to get domain WHOIS information on form submit via Ajax.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php include('templates/header.php');?> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Build Domain WHOIS Lookup System using PHP and Ajax</h2> </div> <form id="lookup-form" class="lookup-form" method="post"> <div class="row align-items-center"> <div class="form-group col-md-9"> <label for="inputEmail4">Domain/IP Address:</label> <input type="text" class="form-control" id="domain" name="domain" placeholder="Domain/IP Address:"> </div> <div class="col"> <button type="button" class="btn btn-primary mt-3 float-left" id="get-lookup"><i class="fa fa-search"></i> WHOIS</button> </div> </div> </form> <div class="row align-items-center"> <div class="form-group col-md-12"> <span id="lookup-dispaly-details"></span> </div> </div> </div> </section> <?php include('templates/footer.php');?> <script type="text/javascript"> jQuery(document).on('click', 'button#get-lookup', function(){ jQuery.ajax({ type:'POST', url:'lookup.php', data:jQuery("form#lookup-form").serialize(), dataType:'html', beforeSend: function () { jQuery('button#get-lookup').button('loading'); jQuery('span#lookup-dispaly-details').html('<i class="fa fa-spinner" style="font-size:30px;"></i>'); }, complete: function () { jQuery('button#get-lookup').button('reset'); }, success: function (html) { jQuery('span#lookup-dispaly-details').html(html); }, error: function (xhr, ajaxOptions, thrownError) { console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); } }); }); </script> |
Step 2: Make Request to Domain WHOIS SERVER — functions
- Handles the Domain WHOIS Lookup process
LookupDomainName()
— Lookup Domain Name.LookupIPAddress()
— LookupIP Address.ValidateIPAddress()
— Validate IP Address.ValidateDomainName()
— Validate Domain Name.getWhoisServerDetails()
— get Whois Server Details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<?php // Lookup Domain Name function LookupDomainName($domain){ global $whoisservers; $domain_parts = explode(".", $domain); $tld = strtolower(array_pop($domain_parts)); $whoisserver = $whoisservers[$tld]; if(!$whoisserver) { return "Error: No appropriate Whois server found for $domain domain!"; } $result = getWhoisServerDetails($whoisserver, $domain); if(!$result) { return "Error: No results retrieved from $whoisserver server for $domain domain!"; } else { while(strpos($result, "Whois Server:") !== FALSE){ preg_match("/Whois Server: (.*)/", $result, $matches); $secondary = $matches[1]; if($secondary) { $result = getWhoisServerDetails($secondary, $domain); $whoisserver = $secondary; } } } return "$domain domain lookup results from $whoisserver server:\n\n" . $result; } // Lookup Ip Address function LookupIPAddress($ip) { $whoisservers = array( "whois.afrinic.net", "whois.lacnic.net", "whois.apnic.net", "whois.arin.net", "whois.ripe.net" ); $results = array(); foreach($whoisservers as $whoisserver) { $result = getWhoisServerDetails($whoisserver, $ip); if($result && !in_array($result, $results)) { $results[$whoisserver]= $result; } } $res = "RESULTS FOUND: " . count($results); foreach($results as $whoisserver=>$result) { $res .= "\n\n-------------\nLookup results for " . $ip . " from " . $whoisserver . " server:\n\n" . $result; } return $res; } // validate IP Address function ValidateIPAddress($ip) { $ipnums = explode(".", $ip); if(count($ipnums) != 4) { return false; } foreach($ipnums as $ipnum) { if(!is_numeric($ipnum) || ($ipnum > 255)) { return false; } } return $ip; } // Validate Domain Name function ValidateDomainName($domain) { if(!preg_match("/^([-a-z0-9]{2,100})\.([a-z\.]{2,8})$/i", $domain)) { return false; } return $domain; } // get Whois Server Details function getWhoisServerDetails($whoisserver, $domain) { $port = 43; $timeout = 10; $fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("Socket Error " . $errno . " - " . $errstr); fputs($fp, $domain . "\r\n"); $out = ""; while(!feof($fp)){ $out .= fgets($fp); } fclose($fp); $res = ""; if((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) { $rows = explode("\n", $out); foreach($rows as $row) { $row = trim($row); if(($row != '') && ($row{0} != '#') && ($row{0} != '%')) { $res .= $row."\n"; } } } return $res; } ?> |
Step 3 : Display Domain WHOIS Information
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $domain = trim($domain); if(substr(strtolower($domain), 0, 7) == "http://") $domain = substr($domain, 7); if(substr(strtolower($domain), 0, 4) == "www.") $domain = substr($domain, 4); if(ValidateIPAddress($domain)) { echo "hello before check"; $result = LookupIPAddress($domain); } elseif(ValidateDomainName($domain)) { $result = LookupDomainName($domain); } else { $result = "Invalid Input!"; } header('Content-Type: application/json'); echo "<pre>\n" . $result . "\n |
Step 4: Make Ajax Request and Load data
Ajax request get domain lookup results from server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<script type="text/javascript"> jQuery(document).on('click', 'button#get-lookup', function(){ jQuery.ajax({ type:'POST', url:'lookup.php', data:jQuery("form#lookup-form").serialize(), dataType:'html', beforeSend: function () { jQuery('button#get-lookup').button('loading'); jQuery('span#lookup-dispaly-details').html('<i class="fa fa-spinner" style="font-size:30px;"></i>'); }, complete: function () { jQuery('button#get-lookup').button('reset'); }, success: function (html) { jQuery('span#lookup-dispaly-details').html(html); }, error: function (xhr, ajaxOptions, thrownError) { console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); } }); }); </script> |
Create files named (header.php and footer.php)
This file contains the header and footer section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header and footer section.
header.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title>Integrate CCAvenue Payment Gateway using PHP | Tech Arise</title> <link rel="icon" type="image/ico" href="https://techarise.com/wp-content/themes/v1/favicon.ico"> <!-- Bootstrap core CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" /> <!-- Custom fonts for this template --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.css" /> <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css"> <!-- Custom styles for this template --> <link href="css/style.css" rel="stylesheet"> </head> <body> <!-- Navigation --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top header-bg-dark" style="background: ##FFFFFF!;"> <div class="container"> <a class="navbar-brand font-weight-bold" href="https://techarise.com"><h1>Tech Arise</h1></a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"> <a class="nav-link" href="https://techarise.com">Home <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="https://techarise.com/php-free-script-demos/">Live Demo</a> </li> </ul> </div> </div> </nav> |
footer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<!-- Footer --> <footer class="footer bg-light footer-bg-dark"> <div class="container"> <div class="row"> <div class="col-lg-6 h-100 text-center text-lg-left my-auto"> <ul class="list-inline mb-2"> <li class="list-inline-item"> <a href="#">About</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Contact</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Terms of Use</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Privacy Policy</a> </li> </ul> <p class="text-muted small mb-4 mb-lg-0">Copyright © 2011 - <?php print date('Y', time());?> <a href="https://techarise.com/">TECHARISE.COM</a> All rights reserved.</p> </div> <div class="col-lg-6 h-100 text-center text-lg-right my-auto"> <ul class="list-inline mb-0"> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-facebook fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-twitter-square fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item"> <a href="#"> <i class="fab fa-instagram fa-2x fa-fw"></i> </a> </li> </ul> </div> </div> </div> </footer> <!-- Bootstrap core JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script> </body> </html> |