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

A brief look at the QueryProcessor class - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Naturally, before I continue adding more methods to the query processor class, it's a good idea to recall how this class was defined. For this reason, below I listed the full source code of the class, as it was written during the previous tutorial:

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

Okay, that's the complete definition for the "QueryProcessor" class. Now, it's time to move on and continue adding more methods to the class. Go ahead and read the next section. 



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