Funnotes Logo
Home Sai Satcharitra Talapatram

WhoIs Server and fsockopen(...) in PHP

[BACK]

Any Software Engineer or a Computer gcience graduate will know what sockets are. They are an important part of communications that can be established between two computers over a public or private domain. It is through these sockets that some domain registrars will make available information about the domain publishers that are available on the public domain (www). The servers that provide this information in this way are called Who Is servers. These servers have a common URL so that they can be located on the public domain very easily. And also there is specific structure for those URLs. They often will be like, whois.domainname.com or whois.domainname.net / .org. Some very commonly available who is servers are whois.networksolutions.com, whois.internic.net.

Sockets are like interfaces that sit between the computer and the public network (i.e., the intranet or internet). These are the communication ports between computers. Any computer that needs to communicate with any other computer in this world needs to communicate through these sockets. These are like electricity sockets to which we plug-in our electrical gadgets. We will consume electricity only by taking supply from those sockets. The same analogy applies to computers and data. They are the ports through which data can be transferred to-and-fro from the computer.

A server can have as many sockets as it can provide; It depends on the capability of the server. However, in general, sockets range lies between 0 and 65,535. In this first 256 ports (or sockets) are for internal use, like for webserver, mail server, file server, etc.

A WhoIs server communicates through socket number 43. This is the port number which listens to requests made to that server about WhoIs information of domains.

The fsockopen(...) in PHP: PHP is a very versatile and easy to use general purpose language, although it is mainly used for web-scripting. PHP provides a function called fsockopen(...) through which a communication channel can be established between the Who Is server and the webserver on which our PHP script runs.

The procedure is very simple.

  • Open a socket on the Who Is server in this way, $fileHandler=(whois.internic.net", 43); just as you do open a file stream in C/C++ using fopen(...) function.
  • Now when we establish a connection by successful execution of this function, we will have its handler as a variable. Like what we have now, the $fileHandler
  • Remember that everything in computer terminology can be considered as a file. Unix/Linux users are well aware of this fact that any device or communication can be considered as a file. Hence our network connection here is also a file which accepts reading and writing as any other normal file. This process is an analogy for receiving and transmitting data through that network connection.
  • Hence with the help of File Handler ($fileHandler) variable that we have here we communicate with the server through fwrite(...) and fread(...) functions.
  • Make a request to the server about the domain name that you want to know information about, in this way, fwrite($fileHandler, "funnotes.net")
  • This operation writes the string "www.funnotes.net" on the socket 43 that we have opened.
  • The server takes this as a request and responds with the information about that domain, as a file, at the same socket.
  • So, now we have to read that socket till we reach the end of the file that whois server has presented at that socket. Below is the PHP code for reading the file and for displaying it.
[BACK][TOP]
	while(!feof($fileHandler))
	{
		$rawData=fread($fileHandler, 256);
		$easyData=explode("\n", $rawData);
		$size=count($easyData);	// find total number of lines
		for($i=0; $i<$size; $i++)
			print "{$easyData[$i]}<br>";
	}
	
  • The function fread(...) is meant to read from the File Hanlder. The function explode(...) detatches the Raw Data at the New Line characters and returns an array with data in separate lines that are actually separated with New Line character in Raw Data. The function count(...) returns the total number of lines that are present in an array (or in other words the dimension of that array). The rest is well known to everyone, a FOR loop displaying the thus formed array, line by line. To read in detail about these functions go to PHP's Manual

With this in place on your mind, now you should be able to write your own script which can read information about a particular domain. But you should be aware and cautious about the permissions that are available to you before you attempt to read. Also you should be aware of the authenticity and warranty that particular who is servers provide. A who is server along with information about a domain, also provides a copytight message with it for you to refer. So, you can certainly have a try. Go to my websites WhoIs page to know information about a particular domain. You can find information about .com, .net and .edu domains only. Good Luck.

V Rama Aravind
27-08-2005.
	

© 2003 - 2023, Rama Aravind Vorray, Inc. Site Last Updated: 2023-04-08. Contact Me