Log in

View Full Version : How To Query Domain Registration



AlistairH
06-17-2005, 03:10 PM
I'm not sure if this is the right forum so I'll apologise in advance.

I'm sure we've all seen the facility to check if a desired domain name is already registered.

Does any one know how I go about providing that facility on my website or is this type of service only available to ISPs? I'm specifically wanting my site visitors to check for the existance of .uk domains which are handled by Nominet

Thanks in advance

Alistair

cr3ative
06-17-2005, 03:44 PM
Try this HTML/PHP hybrid page:

<html>
<head>
<title>PHP WHOIS</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="test.php">
<p>Enter Domain:
<input name="domain" type="text" id="domain">
</p>
<p>
<input type="submit" name="Submit" value="Check Whois">
</p>
</form>
<?php
if(strlen($_POST['domain']) > 0){

// Create Whois Server Array
$server['nic'] = "whois.nic.uk:No match for";
$server['crsnic'] = "whois.crsnic.net:No match for";

// Create Extension Array
$domainInfo['.co.uk'] = "nic";
$domainInfo['.org.uk'] = "nic";
$domainInfo['.ltd.uk'] = "nic";
$domainInfo['.plc.uk'] = "nic";
$domainInfo['.com'] = "crsnic";
$domainInfo['.net'] = "crsnic";
$domainInfo['.org'] = "crsnic";

// Separate Domain and Extension
$fullDomain = $_POST['domain'];
$parts = explode(".",$fullDomain);
$domain = $parts[0];
$extension = str_replace($domain,"",$fullDomain);

// Find Whois Server and Text to Match
$tempServer = $domainInfo[$extension];
$whoisDetails = $server[$tempServer];
$splitWhois = explode(":",$whoisDetails);
$whoisServer = $splitWhois[0];
$textToMatch = $splitWhois[1];

// Query WHOIS Server for Domain
$port = 43;
$domainName = $domain . $extension . "\n";
$fp = fsockopen($whoisServer,43);
fputs($fp, $domainName);
while (!feof($fp))
$whoisRecord .= fgets($fp,128);
fclose($fp);
$whoisRecord = str_replace("\n","<br>",$whoisRecord);

// Check Whether Domain is Registered or not
if(strstr($whoisRecord, $textToMatch) != FALSE){
$registered = 0;
} else {
$registered = 1;
}

// Output Results
if($registered==0){
echo $domain . $extension . " is not registered.";
} else {
echo $domain . $extension . " is registered. WHOIS Details:";
echo "<br><br>";
echo $whoisRecord;
}
}
?>
</body>
</html>

Example at http://b3ta.cr3ation.co.uk/images/test.php

cr3ative

mwinter
06-17-2005, 03:46 PM
I'm not sure if this is the right forum so I'll apologise in advance.The Other (http://www.dynamicdrive.com/forums/forumdisplay.php?f=12) forum would probably have been more appropriate as this is predominately a generic server-side programming question.


Does any one know how I go about providing that [domain name queries] on my website or is this type of service only available to ISPs?You would have to use a registrar's WHOIS service, but you certainly can provide your own query interface. However, parsing the output may not be that simple as I don't think there any guidelines on what exactly is stored, and how it should be returned.

Sending a query is very simple: connect to the WHOIS server on port 43, send the query (with optional flags), followed by a carriage return, line feed (CRLF) pair. You should expect the connection to be automatically closed.

You can read more in RFC 954 (http://www.faqs.org/rfcs/rfc954.html). You'll probably want to just focus on the "Command Lines and Replies" section, as I've just covered the protocol (it really is that simple).


I'm specifically wanting my site visitors to check for the existance of .uk domains which are handled by NominetThen you'll want to use whois.nic.uk.

Note: You must get permission from Nominet before you start using their WHOIS service for more than the occasional lookup. See their disclaimer (http://www.nic.uk/whois/) for a bit more information.

Mike