PHPBuilder - Simple WHOIS Query (XHTML and PHP4.1.2 Complient)



RSS Twitter
Snippets Networking

Simple WHOIS Query (XHTML and PHP4.1.2 Complient)

by: Dan Shepherd
|
June 5, 2002

Version: 1.1

Type: Sample Code (HOWTO)

Category: Networking

License: GNU General Public License

Description: Displays information on .COM, .ORG, .NET and .EDU domains. Output is returned as XHTML and the script is compatable with PHP 4.1.2



<?php

/*
	A Simple WHOIS Checker (c)2001,2002 Danny Shepherd (danny@kyboshed.com)

	Checks Most Domains (but not .tv)
*/

function whois($name,$server='')
{

	if ($server=='')
	{
		$nameParts=explode(".",$name);
		$tld=$nameParts[count($nameParts)-1];
		$server="$tld.whois-servers.net";
	}

	if($socket=fsockOpen($server,43,$errnum,$errstr,30))
	{
		fputs($socket, "$name\r\n");
		$rawData="";
		while(!feof($socket))
		{
			$thisData=fgets($socket,2048);
			$rawData.=$thisData;

			@list($key,$value)=explode(':',strtolower(eregi_replace(" ",'',$thisData)));

			if (isset($key) && isset($value) && $key=='whoisserver')
			{
				$newServer=trim($value);
				break;
			}
		}

		fclose($socket);

		if (isset($newServer))
			$rawData=whois($name,$newServer);

		return $rawData;
	}
	else
		return "Couldn't open connection to: $server ($errnum: $errstr)!!\n";
}

$domain="";
$whoisData="";

if (isset($_REQUEST['domain']) && $_REQUEST['domain']!='')
{
	$domain=$_REQUEST['domain'];
	$whoisData=whois($_REQUEST['domain']);
}

echo <<<EOD
<?xml version="1.0" encoding="iso-8859-1"?>\n
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>Whois Query</title>
	</head>

	<body>
		<form action="{$_SERVER['PHP_SELF']}" method="post">
			Domain Name: <input type="text" name="domain" value="$domain"/>
			<input type="submit" name="submit" value="Check"/>
		</form>

		<hr/>
		<pre>
$whoisData
		</pre>
		<hr/>
	</body>
</html>
EOD;
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Dan Shepherd

Comment:



Comment:

(Maximum characters: 1200). You have characters left.