PHP
  Home arrow PHP arrow Page 5 - 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? 
Google.com  
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 - Gluing the pieces together: listing the full source code of the "QueryProcessor" class
    ( Page 5 of 5 )

    Undoubtedly, you'll have a better idea of how the new methods fit into the structure of the "QueryProcessor" class if you see its full source code, so here is the complete definition of this 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;
        }
        // execute 'ipconfig' command on Windows systems
        public function IpConfig(){
            $output='Running ipconfig command...Please wait.<br />';
            exec('ipconfig',$lines);
            foreach($lines as $line){
                $output.=$line.'<br />';
            }
            return $output;
        }
        // execute 'ping' command on Windows systems
        public function Ping(){
            $output='Running ping command...Please wait.<br />';
            exec('ping '.$this->host,$lines);
            foreach($lines as $line){
                $output.=$line.'<br />';
            }
            return $output;
        }
        // execute 'netstat' command on Windows systems
        public function Netstat(){
            $output='Running netstat command...Please wait.<br />';
            exec('netstat',$lines);
            foreach($lines as $line){
                $output.=$line.'<br />';
            }
            return $output;
        }
        // get MX records on Windows systems
        public function getMXRecordsWin(){
            $output='Retrieving MX Records...please wait.<br />';
            exec("nslookup -type=mx $this->host",$mxhosts);
            foreach($mxhosts as $mxhost){
                $output.=$mxhost.'<br />';
            }
            return $output;
        }
    }

    Now that you've seen the complete source code of the "QueryProcessor" class, below I coded an example that demonstrates how to use some of these new methods:

    // instantiate 'QueryProcessor' object
    $queryProc=new QueryProcessor('google.com');
    // display MX records
    echo $queryProc->getMXRecordsWin();
    /* displays the following output:

    Retrieving MX Records...please wait.
    Server: dns0c.dnserver.com.ar
    Address: 200.51.212.7

    google.com MX preference = 10, mail exchanger = smtp1.google.com
    google.com MX preference = 10, mail exchanger = smtp2.google.com
    google.com MX preference = 10, mail exchanger = smtp3.google.com
    google.com MX preference = 10, mail exchanger = smtp4.google.com

    google.com nameserver = ns3.google.com
    google.com nameserver = ns4.google.com
    google.com nameserver = ns1.google.com
    google.com nameserver = ns2.google.com
    smtp1.google.com internet address = 216.239.57.25
    smtp2.google.com internet address = 64.233.167.25
    smtp3.google.com internet address = 64.233.183.25
    smtp4.google.com internet address = 66.102.9.25
    ns1.google.com internet address = 216.239.32.10
    ns2.google.com internet address = 216.239.34.10
    ns3.google.com internet address = 216.239.36.10
    ns4.google.com internet address = 216.239.38.10

    */

    // display 'netstat' results
    echo $queryProc->NetStat();
    /* displays the following output:

    Running netstat command...Please wait.

     Active Connections

    Proto local address remote address  status
    TCP mywebserver:http localhost:3677 ESTABLISHED
    TCP mywebserver:3677 localhost:http ESTABLISHED

    */

    // display 'ping' results
    echo $queryProc->Ping();
    /* displays the following output:
    pinging google.com [72.14.207.99] with 32 bytes of data:

    Response from 72.14.207.99: bytes=32 time=247ms TTL=237
    Response from 72.14.207.99: bytes=32 time=234ms TTL=237
    Response from 72.14.207.99: bytes=32 time=234ms TTL=237
    Response from 72.14.207.99: bytes=32 time=234ms TTL=238

    Ping statistics for 72.14.207.99:
    Packets: sent = 4, received = 4, lost = 0
    (0% lost),
    Approximated response time en milliseconds:
    Min = 234ms, Max = 247ms, Average = 237ms

    */

    To wrap up

    Over this second article in the series, you learned how to run popular Windows network monitoring utilities, such the "ping", "netstat" and "ipconfig" programs, by adding some new methods to the "QueryProcessor" class. Additionally, you saw how to retrieve the list of MX records corresponding to a given Internet host, by using the "exec()" PHP function, which comes in handy for running system commands.

    Nevertheless, my introduction to PHP programming has not finished yet. In the last tutorial, I'll be defining a few additional methods for the "QueryProcessor" class that can be useful for scanning TCP ports, finding DNS records and much more. Just don't miss it!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek