Sie sind auf Seite 1von 3

Creating a domain checker http://www.phptoys.com/e107_plugins/content/content.php?content.

38

Php resources, scripts and tutorials

Home Products Downloads Tutorials Books Hot scripts Hosting


Tuesday, 15
Ads by Google PHP MySQL Tutorial Domain Bulk Domain Checker PHP Rental Script CA Domain Whois February 2011
19:35
Welcome Hosting Providers
Creating a domain checker
Username:

Password:
by Administrator
Login

Remember me
Ads by Google
[ Signup ]
[ Forgot password? ]
[ Resend Activation Email ]

Recommended
sites PHP Code Generator
PHP Tutorials
PHP 5 web hosting company
Craigs List Best code generator. Forms, Grids
Web Hosting Reviews
CodeConnect.net
Charts,Reports,Menus,Ajax support.
Web Design Toronto
JustHost Reviews
Logo design by Ars
www.scriptcase.net

PHP Hosting

In this tutorial I will show you how to create a PHP script to check whether the actual domain
name is available or not. In the next section I focus on the .com domains but you can extend
the script to handle as many top level domains as you want.

Step 1.
Let’s go through the basics. When you want to check whether a given domain name is
available or not you have to send a request to a whois server which maintains domain
registration details for the actual domain. In general all top level domain (usually just called
tld) has its own whois server. For example a whois server for .com domains is whois.crsnic.net
or for the .info domains it is whois.afilias.net. There can be more whois server for the actual tld.
To get information about the actual domain name we will send a request to the whois server
and then we will process the output. The output show us whether the domain is registered or
not.

Step 2.
After the short introduction let’s create some code. We will create a function which connects to
the whois server using socket connection. Download the response and analyze the output.
Analyzing the output means that we will looking for some special text which indicates whether
the domain is not found. If the response contains a text like “No match for” or “NOT FOUND”
than it means that the actual domain was not registered yet.
So our function will get 3 parameters:
1. Domain name
2. Whois server address
3. Not found text The complete code looks like this:

<?php
function checkDomain($domain,$server,$findText){
// Open a socket connection to the whois server
$con = fsockopen($server, 43);
if (!$con) return false;

// Send the requested doman name


fputs($con, $domain."\r\n");

// Read and store the server response


$response = ' :';
while(!feof($con)) {
$response .= fgets($con,128);
}

// Close the connection


fclose($con);

// Check the response stream whether the domain is available


if (strpos($response, $findText)){
return true;
}
else {
return false;

1 of 3 15/02/2011 19:35
Creating a domain checker http://www.phptoys.com/e107_plugins/content/content.php?content.38

}
}
?>

Explanation:
The code is commented well but I try to explain it a little bit more detailed.

· As first step we try to connect to the whois server on port 43. If the connection fails than we
return to the main program.
· In case of established connection we will send the domain name to the server and read the
response in 128 bytes chunks.
· As we received the complete output then we close the connection. · Check whether the
requested string can be found in the response text. So we check if the string “No match for” is
there or not.
· If the string was found it means that the domain is not registered yet otherwise it is taken
already.

Step 3.
Now we have a function which can check a domain is available or not. To make a useful script
it would be nice to make a nice html page around it where a user can input the requested
domain and as result he/she get the availability. So let’s create a HTML page with a form which
contains an input field. Besides this I added a checkbox for the tld to make it easier to extend
the script. The HTML code with the built in form processing looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-


transitional.dtd">
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="domain">
Domain name:
<table>
<tr><td><input name="domainname" type="text" /></td></tr>
<tr><td><input type="checkbox" name="com" checked/>.com</td></tr>
<tr><td><input type="submit" name="submitBtn" value="Check domain"/>
</td></tr>
</table>
</form>
<?php
// The form was submitted
if (isset($_POST['submitBtn'])){
$domainbase = (isset($_POST['domainname'])) ? $_POST['domainname'] : '';
$d_com = (isset($_POST['com'])) ? 'com' : '';

// Check domains only if the base name is big enough


if (strlen($domainbase)>2){
echo '<table width="100%">';
if ($d_com != '')
showDomainResult($domainbase.".com",'whois.crsnic.net','No match for');
echo '</table>';
}
}
?>
</body>

Explanation: The code is quite easy. The first part contains the HTML form code which as action
calls the script itself. There are 3 table rows. One for the domain name, one for the tld and one
the submit button. If the form was submitted – the POST array has the “submitBtn” in it – than
we set the domain name and the tld in 2 separate variables. After it we make a small check
whether the domain name is long enough. Here you can introduce more checks to avoid any
other not valid inputs. As next step the script begins to output a table with the result. Here a
function showDomainResult() is called which calls our main function and outputs result in a table
row. The code of the showDomainResult() function looks like this:

<?php
function showDomainResult($domain,$server,$findText){
if (checkDomain($domain,$server,$findText)){
echo "<tr><td>$domain</td><td>AVAILABLE</td></tr>";
}
else echo "<tr><td>$domain</td><td>TAKEN</td></tr>";
}
?>

Step 4. Now we are ready. You have a basic script which can check a .com domain
availability. You can extend it with adding new checkboxes to the form for the new tlds and call
the showDomainResult() function with the new parameters. Finally here is the complete script:

<?php
function checkDomain($domain,$server,$findText){
// Open a socket connection to the whois server
$con = fsockopen($server, 43);
if (!$con) return false;

// Send the requested doman name


fputs($con, $domain."\r\n");

// Read and store the server response


$response = ' :';

2 of 3 15/02/2011 19:35
Creating a domain checker http://www.phptoys.com/e107_plugins/content/content.php?content.38

while(!feof($con)) {
$response .= fgets($con,128);
}

// Close the connection


fclose($con);

// Check the response stream whether the domain is available


if (strpos($response, $findText)){
return true;
}
else {
return false;
}
}

function showDomainResult($domain,$server,$findText){
if (checkDomain($domain,$server,$findText)){
echo "<tr><td>$domain</td><td>AVAILABLE</td></tr>";
}
else echo "<tr><td>$domain</td><td>TAKEN</td></tr>";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-


transitional.dtd">
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="domain">
Domain name:
<table>
<tr><td><input name="domainname" type="text" /></td></tr>
<tr><td><input type="checkbox" name="com" checked/>.com</td></tr>
<tr><td><input type="submit" name="submitBtn" value="Check domain"/>
</td></tr>
</table>
</form>
<?php
// The form was submitted
if (isset($_POST['submitBtn'])){
$domainbase = (isset($_POST['domainname'])) ? $_POST['domainname'] : '';
$d_com = (isset($_POST['com'])) ? 'com' : '';

// Check domains only if the base name is big enough


if (strlen($domainbase)>2){
echo '<table>';
if ($d_com != '')
showDomainResult($domainbase.".com",'whois.crsnic.net','No match for');
echo '</table>';
}
}
?>
</body>

Tags:

Php Toys - 2006 - Php resources, scripts and tutorials - Privacy Policy
Insurance Index - Tutorial collection - Forex trading, brokers, reviews - Mortgage payment calculator
{THEMEDISCLAIMER}

Render time: 0.1904 sec, 0.0151 of that for queries. DB queries: 26.

3 of 3 15/02/2011 19:35

Das könnte Ihnen auch gefallen