HomePHP Page 4 - Email Address Verification with PHP
Customizing checkdnsrr() for Windows - PHP
Many applications in the field of Web development need to validate email addresses. While this can be done in a variety of ways, one simple but effective way involves writing your own functions in PHP. Alejandro Gervasio explains this approach.
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:
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:
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:
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.