developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See The Winners!




Developer Jobs

Be a Commerce Partner














 


Related Article -
Learning PHP
Creating a Custom RSS Feed Aggregator
Searching Active Directory with Perl
Integrating Active Directory with PHP
Developer News -
Why Firefox Doesn't Take Google Chrome Features    June 26, 2009
First Major PHP Update in Years Coming Soon    June 25, 2009
Red Hat CEO Calls on Oracle to Keep Java Open    June 25, 2009
Google Widens AdSense for iPhone, Android Apps    June 24, 2009
Free Tech Newsletter -

Validating Emails with PHP
By W. Jason Gilmore

Go to page: Prev  1  2  

Validating Domain Existence

Once the email's syntax is validated, it's time to ensure that the domain exists and is configured to accept mail. The easiest way to do so is to verify that an MX (Mail Exchange) record for that domain exists. You can do so easily using PHP's getmxrr() function, introduced here.

getmxrr()

int getmxrr(string hostname array mxrr [, array weight])

The getmxrr() function will contact a DNS server in an attempt to determine whether MX (mail exchange) records for that host exist, returning TRUE if the records are found, and FALSE otherwise. If records are found, they are placed within the input parameter mxrr. If the optional weight array parameter is included, then the respective weight attributes for each record are placed there. However, because we're only interested in determining whether MX records for the given domain exist, we can invoke getmxrr() like so:

<?php
   $email = "johnny.rocket@example.com";
   list($username,$domain) = split("@",$email);
   if (getmxrr($domain,$mxrecords))
      echo "Email domain exists!";
   else
      echo "Email domain does not exist!";
?>

Putting it All Together

Let's assemble both the syntax and existence validation logic. To encourage reuse, the logic is incorporated into a function named validate_email(), which accepts as input a single parameter, $email. The function, along with a usage example is presented here:

<?php

function validate_email($email)
{

   // Create the syntactical validation regular expression
   $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";

   // Presume that the email is invalid
   $valid = 0;

   // Validate the syntax
   if (eregi($regexp, $email))
   {
      list($username,$domaintld) = split("@",$email);
      // Validate the domain
      if (getmxrr($domaintld,$mxrecords))
         $valid = 1;
   } else {
      $valid = 0;
   }

   return $valid;

}

$email = "johnny-rocket@example.com";

if (validate_email($email))
   echo "Email is valid!";
else
   echo "Email is invalid!";

?>

Conclusion

Given the mission-critical importance of communicating order confirmations, download instructions, newsletters and the like via email, taking the steps necessary to validate such user-input items will be well worth your effort. As you've learned in this tutorial, the ease in which such a feature can be implemented really leaves you with no real excuse for not doing so! If you wind up adding email validation to your application as a result of this tutorial, I'd love to hear about it! Please email me at jason@wjgilmore.com with all the details.

About the Author

W. Jason Gilmore (http://www.wjgilmore.com/ ) is an Internet application developer for the Fisher College of Business. He's the author of the upcoming book, PHP 5 and MySQL: Novice to Pro, due out by Apress in 2004. His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed, Zend.com, and Webreview. Jason is also the author of A Programmer's Introduction to PHP 4.0 (453pp., Apress). Along with colleague Jon Shoberg, he's co-author of "Out in the Open," a monthly column published within Linux magazine.

# # #

Go to page: Prev  1  2  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


PHP Archives






internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs