Home arrow PHP arrow Page 2 - Adding Methods to the Query Processor in PHP

Reviewing previous PHP networking functions: a quick look at the "QueryProcessor" class - PHP

Over this second article in a series covering network programming in PHP, you will learn how to run popular Windows network monitoring utilities, such as the “ping,” “netstat” and “ipconfig” programs, by adding some new methods to the “QueryProcessor” class introduced in the previous article.

TABLE OF CONTENTS:
  1. Adding Methods to the Query Processor in PHP
  2. Reviewing previous PHP networking functions: a quick look at the "QueryProcessor" class
  3. Adding more functionality to the "QueryProcessor" class: defining the "Ping()" and "IpConfig()" methods
  4. More methods ahead: defining the "Netstat()" and "getMXRecordsWin()" methods
  5. Gluing the pieces together: listing the full source code of the "QueryProcessor" class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
May 24, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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