PHP
  Home arrow PHP arrow Page 4 - Completing a Query Processor in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Completing a Query Processor in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2006-05-31


    Table of Contents:
  • Completing a Query Processor in PHP
  • A brief look at the QueryProcessor class
  • Expanding the functionality of the QueryProcessor class: defining the getMXRecords() and checkDNSRecords() methods
  • Scanning TCP ports, running a WHOIS service and more: defining the scanPort() and getWhois() methods
  • Getting the class completed: listing the full source code of the QueryProcessor class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Completing a Query Processor in PHP - Scanning TCP ports, running a WHOIS service and more: defining the scanPort() and getWhois() methods
    ( Page 4 of 5 )

    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){
        if(!is_int($port)||$port<1||$port>65535){
            throw new Exception('Invalid TCP port number.');
        }
        $output='Scanning port '.$port.'...please wait.<br />';
        if(!$fp=@fsockopen($this->host,$port,$errno,$errstr,30)){
            throw new Exception('Unable to open connection to port
    '.$port);
        }
        fclose($fp);
        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.');
        }
        sleep(2);
        fputs($fp,"$this->hostrn");
        while(!feof($fp)){
            $output.=fread($fp,128).'<br />';
        }
        fclose($fp);
        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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT