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