Helping ordinary people create extraordinary websites!
$1 CPM Advertising For A Limited Time Only
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Verify a User’s Email Address with PHP

By Darren W. Hedlund
2005-03-29


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.

Tutorial Pages:
» Verify a User’s Email Address with PHP


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.