Location: PHPKode > scripts > DCHK > dchk/dchk.php
<?php

/**
 * DCHK - PHP Class to check if a domain name exists or is available
 *
 * Tested and working on PHP 4.3.2 and higher
 *
 * LICENSE: This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License v2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * @author       Giulio Bai <hide@address.com>
 * @copyright   (C)2007 Giulio Bai - GNU GPL
 * @license       http://www.gnu.org/licenses/gpl.html GNU General public License
 * @version      1.0
 * @link            http://hewle.com
 */
 

/**
 * A class to check the presence of a domain name, having any extension.
 * 
 * Example of use (1):
 * The following code will check for the domain name example.com and, if available,
 * will return the link to the registration through godaddy.com
 * You can change the server used to check domains availability and the site
 * from where buy the domain name by specifing them in the method call. 
 * <code>
 * include_once('dchk.php');
 *
 * $free = new DCHK();
 * if($free->check_domain("example.com"))
 *     // domain available
 * else
 *     // Domain already registered!
 * </code>
 *
 * Example of use (2):
 * Form to check domain names
 * <code>
 * <?php
 * if (isset($_POST['submit'])) {
 *     $domain = $_POST['domain'];
 *     $ext = $_POST['extension'];
 *     $full_name = $domain . '.' . $ext;
 *
 *     include_once('dchk.php');
 * 
 *     $free = new DCHK();
 *     if($free->check_domain("example.com, , , 1")) // Prints the register link also
 * } else {
 *     <form>
 *         Check for a domain name:
 *         <input type="text" name="domain" />
 *         <select name="extension">
 *             <option value="com">.com</option>
 *             <option value="org">.net</option>
 *             <option value="net">.org</option>
 *             <option value="biz">.biz</option>
 *         </select>
 *         <br />
 *         <input type="submit" name ="submit" value="Check now!"/>
 *     </form>
 * ?>
 * }
 * </code>
 *
 * @author      Giulio Bai <hide@address.com>
 * @copyright  (C)2007 Giulio Bai
 * @license      http://www.gnu.org/licenses/gpl.html GNU General Public License
 * @version     1.0
 * @link           http://hewle.com
 */ 
class DCHK
{
	
	/**
     * Server from which check the domain availability.
     *
     * @var string
     */ 
	var $server = "whois.networksolutions.com";
	
	/**
     * Port to access the server
     *
     * @var int
     */ 
	var $port = 43;
	
	/**
     * Registration link. Displayed if the domain is available.
	 *
	 * You can replace this link with another one, at your choise, but remember the
	 * domain name will be added in substitution of '$domain'
     *
     * @var string
     */ 
	var $reg_link = "http://order.1and1.co.uk/xml/order/domaincheck?__sendingdata=1&__pageflow=Order&multicheck.Domain=$domain";
	
	/**
     * No match string. Usually you do not need to modify.
     *
     * @var string
     */ 
	var $nomatch = "No match for";
	
	/**
     * Output of the test.
     *
     * @var string
     */ 
	var $output = "";
	
	
	/**
     * Checks the domain avaiability and, if specified, prints the result.
     *
     * @param string $domain the domain to check
     * @param string $server the server used for the checking
	 * @param int      $port the port needed to reach the server
	 * @param bool   $print set to 1 if you want the outputs printed
     *
     * @return bool returns true if the domain is available, false otherwise
     */ 
	function check_domain($domain, $server = $this->server, $port = $this->port, $print = 0)
	{
		$this->server = $server;
		$this->port = $port;
		
		$e = explode(".", $domain);
		
		if (count($e) != 2) return true; // Strange domain name, uh?
		
		$domain = $e[0];
		$ext = $e[1];

		if(($sc = fsockopen($this->server, $this->port)) == false) 
			return true;

		fputs($sc, "$domain . $ext\n");

		while(!feof($sc))
			$this->output .= fgets($sc, 128);
		
		fclose($sc);
		
		if (eregi($this->nomatch, $this->output)) {
			if ($print)
				echo '<a href="' . $this->reg_link .'">Register now ' . $domain . '!</a>';
			
			return true;
		} else {
			if ($print)
				echo 'Sorry, the domain ' . $domain . ' is already registered!';
				
			return false;
		}
	}

}

?>
Return current item: DCHK