Email Address Verification with PHP - Customizing checkdnsrr()
(Page 4 of 6 )
The customCheckDnsrr() function is a classical solution for implementing the desired functionality of checkdnsrr() on a Windows platform; it is extensively used across numerous scripts. The code for our new function is as follows:
function customCheckDnsrr($host,$recType='') {
if(!empty($host)) {
if($recType=='') $recType="MX";
exec("nslookup -type=$recType $host",$output);
foreach($output as $line) {
if(preg_match("/^$host/", $line)) {
return true;
}
}
return false;
}
return false;
}
Our version of the checkdnsrr() function works by making a system call, available in Windows systems, known as nslookup. It resembles the chekdnsrr() functionality, and is very useful for achieving the same results. We make use of the nslookup function by invoking the PHP exec() function, which is one of the several methods for executing a system command in PHP. The result of the command is stored as an array in the $output parameter.
When the nslookup function is executed, it searches the corresponding entry in the DNS for the given domain. If the result is successful, the output is similar to the following lines:
Server: ns1.infoar.net
Address: 200.80.203.242
calop.com.ar MX preference = 10, mail exchanger = mail.infoar.net
To determine whether a proper mail handler for that domain exists, the function iterates over each line of the output by searching for the line that begins with the provided host name. If the line is found, then the function will return true. Otherwise, it will return false.
Here’s the code for using our customCheckDnsrr() function:
function checkEmail($email) {
if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( \.[a-zA-Z0-9_-] +)+$/" , $email)){
list($username,$domain)=split('@',$email);
if(!customCheckDnsrr($domain)){
return false;
}
return true;
}
return false;
}
The snippet is almost identical to the one using checkdnsrr(). It has only been replaced with the customized function, previously defined.
So far, we have a working function to be properly implemented under Windows platforms. Similarly, we might replace checkdnsrr() with PHP’s getmxrr() within our checkEmail() function. Let’s see this new alternative to determine the validity of an email address’ domain in more detail.
Next: Using getmxrr() for validation >>
More PHP Articles
More By Alejandro Gervasio
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|