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

Expanding the functionality of the QueryProcessor class: defining the getMXRecords() and checkDNSRecords() methods - 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

I'm sure you'll remember that in the second part of the series, I defined a class method called "getMXRecordsWin()." The functionality of this method was basically retrieving the list of MX records corresponding to an Internet host, in case you're working with a Windows-based system. Now, I'll show you the definition of a method that does essentially the same thing, but uses the "getmxrr()" PHP function, which as you know, isn't available in Windows. The signature for this method is as follows:

// get MX records
public function getMXRecords(){
    if(getmxrr($this->host,$mxhosts)){
        throw new Exception('No MX Records were found.');
    }
    $output='Retrieving MX Records...please wait.<br />';
    foreach($mxhosts as $mxhost){
        $output.=$mxhost.'<br />';
    }
    return $output;
}

In this case, the above method tries to obtain the list of MX records that match the given host name (when applicable), and next loops over the resulting array. Finally the output is returned to calling code.

After defining the "getMXRecords()" method, have a look at this new one, tasked with checking whether there are records in the DNS for a specific Internet host:

// check for DNS records
public function checkDNSRecords($recType='MX'){
    // allowed values for $recType
    $validRecTypes=array
('A','MX','NS','SOA','PTR','CNAME','AAAA','A6','SRV',
'NAPTR','ANY');
    if(!in_array($recType,$validRecTypes)){
        throw new Exception('Invalid DNS record type.');
    }
    if(!checkdnsrr($this->host,$recType)){
        throw new Exception('No '.$recType.' records were
found.');
    }
    return 'DNS records where found.';
}

As you can see, the method listed above checks in the DNS to see whether there are records that match the given host name, by using the "checkdnsrr()" PHP native function. Also, the method verifies whether the type of record passed as argument is valid or not, and throws an exception if no records are found in the DNS.

In addition, since the "checkdnsrr()" function is not supported on Windows systems, I coded a new method that can be used with Windows machines, and performs the same operation that you saw before. This is how this method looks:

// check for DNS records on Windows systems
public function checkDNSRecordsWin($recType='MX'){
    // allowed values for $recType
    $validRecTypes=array
('A','MX','NS','SOA','PTR','CNAME','AAAA','A6','SRV',
'NAPTR','ANY');
    if(!in_array($recType,$validRecTypes)){
        throw new Exception('Invalid DNS record type.');
    }
    exec('nslookup -type='.$recType.' '.$this->host,$result);
    foreach($result as $line){
        if(preg_match("/^$this->host/",$line)){
            return 'DNS records were found.';
        }
    }
    throw new Exception('No '.$recType.' records were found.');
}

As I mentioned before, this method checks for the existence of records in the DNS for a specific Internet host, and can be run on Windows-based computers, so depending on the operating system you're using, you can utilize this method or the previous one for performing a quick DNS search.

At this point, you have seen three new methods that demonstrate how to look for certain records in the DNS, in accordance with the operating system being used. However, there are still more methods to add to the "QueryProcessor" class, in order to get it completed. Therefore, the next section of this article will show you the definition of these additional methods, responsible for scanning TCP ports, running a basic "WHOIS" service and more. Thus, click on the link below and keep reading.



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