PHP
  Home arrow PHP arrow Page 2 - Adding Methods to the 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

Adding Methods to the Query Processor in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2006-05-24


    Table of Contents:
  • Adding Methods to the Query Processor in PHP
  • Reviewing previous PHP networking functions: a quick look at the "QueryProcessor" class
  • Adding more functionality to the "QueryProcessor" class: defining the "Ping()" and "IpConfig()" methods
  • More methods ahead: defining the "Netstat()" and "getMXRecordsWin()" methods
  • Gluing the pieces together: 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


    Adding Methods to the Query Processor in PHP - Reviewing previous PHP networking functions: a quick look at the "QueryProcessor" class
    ( Page 2 of 5 )

    Before I proceed to add some additional methods to the PHP 5 "QueryProcessor" class that you saw previously, allow me first to list its source code, as it was defined originally in the first article. Here is the signature for the "QueryProcessor" class:

    class QueryProcessor{
        private $host;
        private $services=array
    ('http','https','ftp','telnet','imap','smtp','nicname','gopher',
    'finger','pop3','www');
        private $ports=array(21,23,25,43,70,79,80,110,143,443);
        public function __construct($host='myhost.com'){
            $this->host=$host;
        }
        // get IP address
        public function getIp(){
            if(!$ip=gethostbyname($this->host)){
                throw new Exception('Error resolving host IP
    address.');
            }
            return $ip;
        }
        // get list of IP addresses
        public function getIpList(){
            if(!$ips=implode(' - ',gethostbynamel($this->host))){
                throw new Exception('Error getting list of IP
    addresses for the provided hostname.');
            }
            return $ips;
        }
        // get host name
        public function getHost(){
            if(!$host=gethostbyaddr($this->getIp())){
                throw new Exception('Error resolving host name.');
            }
            return $host;
        }
        // get TCP ports of Internet services
        public function getServicePorts(){
            $output='Retrieving services ports...Please wait.<br />';
            foreach($this->services as $service){
                if(!$port=getservbyname($service,'tcp')){
                    $output.='Error retrieving port of service
    '.$service.'<br />';
                }
                else{
                    $output.='Service '.$service. ' runs on TCP
    port :'. $port.'<br />';
                }
            }
            return $output;
        }
        // get Services by TCP ports
        public function getServiceNames(){
            $output='Retrieving services names...Please wait.<br />';
            foreach($this->ports as $port){
                if(!$service=getservbyport($port,'tcp')){
                    $output.='Error retrieving service name on port
    '.$port.'<br />';
                }
                else{
                    $output.='TCP Port '.$port. ' is used by
    service :'. $service.'<br />';
                }
            }
            return $output;
        }
    }

    As you can see, the "QueryProcessor" class has a few handy methods, which use some of the most common PHP networking functions. With regards to this, the class is capable of resolving a specific host name to its corresponding IP address(or eventually a list of IP addresses), as well as performing the inverse process, that is given an IP address, turning it to the respective Internet host.

    In addition, the query processor exposes two extra methods. The first one is focused on retrieving the number of the TCP port that corresponds to a specific service, while the second one performs the inverse process, obtaining the name of a service provided at a given TCP port.

    Having covered at a glance how the "QueryProcessor" class was originally defined, here is an example that shows how to use its methods:

    try{
        // instantiate 'QueryProcessor' object
        $queryProc=new QueryProcessor('hotmail.com');
        // display host IP address
        //echo $queryProc->getIp(); // displays 64.4.32.7
        // display host name
        //echo $queryProc->getHost(); // displays 'hotmail.com.br'
        // display IP list
        //echo $queryProc->getIpList(); // displays 64.4.32.7 -
    64.4.33.7
        // display services ports
        echo $queryProc->getServicePorts();
        /* displays the following list
        Retrieving services ports...Please wait.
        Service http runs on TCP port :80
        Service https runs on TCP port :443
        Service ftp runs on TCP port :21
        Service telnet runs on TCP port :23
        Service imap runs on TCP port :143
        Service smtp runs on TCP port :25
        Service nicname runs on TCP port :43
        Service gopher runs on TCP port :70
        Service finger runs on TCP port :79
        Service pop3 runs on TCP port :110
        Service www runs on TCP port :80
        */
        // display service names
        echo $queryProc->getServiceNames();
        /* displays the following list
        Retrieving services names...Please wait.
        TCP Port 21 is used by service :ftp
        TCP Port 23 is used by service :telnet
        TCP Port 25 is used by service :smtp
        TCP Port 43 is used by service :nicname
        TCP Port 70 is used by service :gopher
        TCP Port 79 is used by service :finger
        TCP Port 80 is used by service :http
        TCP Port 110 is used by service :pop3
        TCP Port 143 is used by service :imap
        TCP Port 443 is used by service :https
        */
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();

    As shown in the above example, once an Internet host name has been passed to the class constructor, the subsequent calls to the respective methods demonstrate how to obtain the corresponding IP address for that host, or the full list of IP addresses. They also display the TCP ports associated with specific services and, inversely, show the service that runs on a given port.

    As you can see, most of these class methods are very simple, but you can see how they can be used as the building blocks of larger networking PHP applications.

    Now, it's time to continue expanding the existing functionality of the "QueryProcessor" class, so please click on the link below in order to learn how you can implement the popular "ping" and "ipconfing" Windows utilities as new class methods.



     
     
    >>> 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