Web Development

Verify a User’s Email Address with PHP

Verify a User’s Email Address with PHP

So you thought that it would be nice to publish a monthly newsletter for your ever growing web site. You create the registration page, and before you know it, your database is being filled with eager email address just waiting for your newsletter. Further, you notice that your favorite superheroes are among your top subscribers. Superman@hahaha.com and Cat-Woman@youraloser.com seem to have found their way onto your mailing list. So what the hell are you going to do about it?

I may have one solution for you. It is possible to check and make sure that the domain a user has “registered” their email address with is actually a valid domain name. To pull off this little stunt, we are going to have to look at the checkdnsrr() function. This bad boy will check the domain registration for Superman’s email address, and make sure he did not misspell it.

The checkdnsrr function has the following format: (for more information on this function, please visit the PHP Homepage at http://www.php.net)

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

It is important to note that the string type is an optional field. If you happen to leave it blank, the default type is assigned (“MX” meaning Mail Exchange). However, since we want our code to be as clear, and professional as possible, I am going to use it throughout the rest of this tutorial.

Note: Other types you may need for future scripts include A, NS, SOA, PTR, CNAME, ANY, or AAAA.

Note: The checkdnsrr function cannot (as of yet) be used on a Windows Operating System.

Again, the checkdnsrr function will scan for the DNS records specified in an email address. If any matches are found, the function returns the value TRUE. If no records are found of the DNS Domain Name, the function returns the value FALSE.

Now, before we can begin to check for records of the domain registration, we need to split the email in two. Basically, we need to get youraloser.com by itself, away from Cat-Woman. In order to do so, we can use the split() function provided for us so graciously by the masterminds behind PHP.

What is so beautiful about this function is its ability to split an array, or string into sections, based on patters found within the subject matter. A date written as 04/04/03 has a pattern of its own, as does an email address (Superman@hahaha.com). Each is split into sections by a common character, whether that character is a slash, or the @ symbol. The string function has the following format:

array split ( string pattern, string [, int limit]);

In order to check an email address, we are going to have to split the address in two, and run it through the checkdnsrr function as so:

<?php

$emailaddress = “Superman@hahaha.com”;
// First we define $email

list($user, $domain) = split(“@”, $emailaddress);
// Now we split Superman and hahaha.com

if (checkdnsrr($domain, “MX”)) {
// If checkdnsrr returns TRUE
} else {
// If checkdnsrr returns FALSE
}
?>

Now, if the checkdnsrr function returns TRUE we know we have a valid domain name. However, the world is not necessarily a perfect place, and therefore we were not able to check the username. Oh well, I guess this will have to make do.

About the author

Written by Darren W..

Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design.

Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.

If you found this post useful you may also want to check these out:

  1. How To Send Email With Perl, Part I
  2. How To Send Email With Perl, Part II
  3. How To Send Email With Perl, Part III
  4. Sending Cookies By Email
  5. The PHP Explode Function, Split a String by String
  6. A Domain Name & Web Site – The Key To The Door.