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

Validating the proper format of an email address - 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

The first step to validating an email address is to check whether it is in the standard format. If you’re a seasoned programmer, feel free to skip over this description. However, for the sake of giving a complete explanation, we’re going to begin by defining the first checking function, which takes advantage of PHP’s built-in support for regular expressions:

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;
}

Nothing unexpected, right? The simplistic checkEmail() function validates the format of a user’s email address by encoding its standardized format in a regular expression. The preg_match() PHP built-in function looks for matches to the email pattern, given the string $email passed as a parameter. If matches are found, the function will return true. Otherwise, it will return false. It’s a very simple concept, really.

A bit of analysis clearly shows that many invalid email addresses passed as an argument will still match this regular expression. This will result in the function returning true, and considering the addresses as valid data. Though it may be impossible to catch all of the email addresses this way, performing validation routines on the format itself can improve our overall checking process.

Since this function on its own is insufficient for checking whether an email address is valid, we need to look for other ways of improving the validation process. The next step is to check whether an email address corresponds to a real domain by making sure there is a domain registration record for the domain that the user entered. How we achieve that is the subject of the next section.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: