Home arrow PHP arrow Page 4 - Completing a Query Processor in PHP

Scanning TCP ports, running a WHOIS service and more: defining the scanPort() and getWhois() methods - PHP

Welcome to the third part of the series “Network Programming in PHP.” In three tutorials, this series explains the basics of network programming in PHP, by developing a query processor class in PHP 5, which uses some of the most popular PHP network functions, in order to implement the logic of its methods.

  1. Completing a Query Processor in PHP
  2. A brief look at the QueryProcessor class
  3. Expanding the functionality of the QueryProcessor class: defining the getMXRecords() and checkDNSRecords() methods
  4. Scanning TCP ports, running a WHOIS service and more: defining the scanPort() and getWhois() methods
  5. Getting the class completed: listing the full source code of the QueryProcessor class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
May 31, 2006

print this article



The last two methods of the "QueryProcessor" class tackle different tasks. The first one, "scanPort()," not surprisingly checks whether a given TCP port is available for connections, and its source code is the following:

// scan TCP ports
public function scanPort($port=80){
        throw new Exception('Invalid TCP port number.');
    $output='Scanning port '.$port.'...please wait.<br />';
        throw new Exception('Unable to open connection to port
    return 'Port '.$port.' is open to connections.';

As shown above, the "scanPort()" method verifies whether or not a specific TCP port is open to connections by using the powerful "fsockopen()" PHP function. In this case, if the port in question isn't available, a new exception is thrown. Otherwise, a string is returned, confirming that the connection has been successfully established.

Now that you know how the "scanPort()" method works, turn your attention to the last method of the class, called "getWhois()," which implements a basic "WHOIS" service. Please take a look at its source code:

// get basic WHOIS info
public function getWhoIs(){
    $output='Retrieving WHOIS information...please wait.<br />';
    if(!$fp=fsockopen("whois.opensrs.net",43,$errno, $errstr,30)){
        throw new Exception('Error opening socket connection to
WHOIS server.');
        $output.=fread($fp,128).'<br />';
    return $output;

Surely, you'll agree with me the above method is really easy to understand. What it does essentially is implement a basic WHOIS service, by opening a socket connection to the "whois.opensrc.net" WHOIS host. Of course, this is merely an example of how to build this kind of service, and should be used with caution and responsibility. Eventually, you may want to change the WHOIS server and use another one.

Similar to the previous methods that you learned, if the connection to the host is successfully established, the server response is returned to calling code, while if the connection fails, an exception is thrown. Simple, right?

At this stage, I think all the methods I coded earlier complete the "QueryProcessor" class. Now, it's time to see the full source code for this class, after adding the additional methods I wrote before. Want to see the complete definition for the query processor? Go ahead and read the next few lines.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: