Home arrow PHP arrow 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.

  1. Email Address Verification with PHP
  2. Validating the proper format of an email address
  3. Validating email domains with checkdnsrr()
  4. Customizing checkdnsrr()
  5. Using getmxrr() for validation
  6. Empowering validation with fsockopen()
By: Alejandro Gervasio
Rating: starstarstarstarstar / 215
February 08, 2005

print this article



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
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)){
   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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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