Home arrow PHP arrow Page 3 - Email Address Verification with PHP

Validating email domains with checkdnsrr() - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In order to check whether a user’s email address actually corresponds to a real domain, we should search for the proper domain records in the DNS. By doing so, we’re making sure that the supplied email address belongs to an existing domain. To do this, we can use a couple of PHP lookup functions that come in handy for addressing these problems.

The checkdnsrr() function checks DNS records corresponding to a given Internet host name or IP address. It searches the DNS for records of a specific type corresponding to the given host, returning true if any records are found, or returning false if no records are found or if an error occurs.

It has the following format:

int checkdnsrr ( string host [, string type]) ;


The function accepts the following types of records: A, MX, NS, SOA, PTR, CNAME, or ANY, with MX (Mail Exchange) as the default type. So, if no type is provided, the function will search MX records for the given host. In our case, we need to look for MX records according to the host provided within the email address. Therefore, it‘s pretty easy to code a new function, which will take care of checking the existence of the corresponding MX entries for a given host. Let’s write the function to do that:

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(!checkdnsrr($domain,'MX')) {
   return false;
  }
  return true;
 }
 return false;
}

The above function accepts a string as an email parameter for checking whether it fits the proper format, and whether the domain is real. In order to obtain the domain part, we split the email address into the username and domain sections, respectively, using the PHP’s split() function, as listed below:

list($username,$domain)=split(‘@’,$email);

Now, the $domain variable stores the corresponding domain. Since we’re interested only in this, all we need to do is pass it in as a parameter for the PHP chekdnsrr() function to determine whether it’s a real domain. It will look for the Mail Exchange record in the DNS (remember that the default type is MX), and return true if a MX record is found, which shows that the address displays a valid email domain. If the function returns false, the email domain is not valid. Obviously, an email address that displays a real email domain doesn’t necessarily imply that the user name is valid. We have a big challenge ahead.

Once we have defined the new function, we could call it this way, assuming that we have an incoming email address from a POST form:

$email = trim($_POST['email']);  
if(!checkEmail($email)) { 
echo 'Invalid email address!';
}
else {
 echo 'Email address is valid';
}

Our function is very easy to implement and powerful enough to handle the problem of verifying that we’re dealing with an existing domain. However, as stated in the PHP manual, the checkdnsrr() function is not implemented on Windows platforms. It would be useful to have a version that works on Windows for those developers building applications to be executed on Windows servers.

There are some workarounds to deal with this. Most of these involve writing a custom version of the checkdnsrr() function. So let’s move on and write some code for this Windows-based function.



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