PHP
  Home arrow PHP arrow Page 5 - Email Address Verification with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Email Address Verification with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 187
    2005-02-08


    Table of Contents:
  • Email Address Verification with PHP
  • Validating the proper format of an email address
  • Validating email domains with checkdnsrr()
  • Customizing checkdnsrr()
  • Using getmxrr() for validation
  • Empowering validation with fsockopen()

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Email Address Verification with PHP - Using getmxrr() for validation
    ( Page 5 of 6 )

    It’s possible to use the getmxrr() PHP function to achieve email domain validation similar to that obtained with checkdnsrr(). This function gets MX records corresponding to a given Internet host name and has the following format:

    int getmxrr ( string hostname, array mxhosts [, array weight]);

    It searches the DNS for MX records corresponding to the given host name. It returns true if any records are found and returns false if no records are found or if an error occurs.

    A list of the MX records found is placed into the array mxhosts. If the weight array is given, it will be filled with the weight information gathered.

    Having presented this network function, it’s easily deducible that we could rewrite our checkEmail() function utilizing getmxrr() instead of checkdnsrr() to validate email domains. The revamped version is listed below:

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

    And we call it in the following manner:

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

    This code looks very similar to the previous example using checkdnsrr(). The only subtle difference lies in that we have wrapped getmxrr() into the checkEmail() function. It’s a simple but powerful solution. As we can appreciate, the set of PHP network functions is an invaluable tool for validating email domains.

    So far, we have defined individual functions to first, validate the proper format for an email address, and next, check whether the email domain is a real domain. From a strict point of view, this solution is still incomplete, because we really don’t know if the given user name is valid. How can this issue be addressed properly?

    Actually, there is no direct way to do that. However, a fairly handy approach to help us see whether we are dealing with a valid user name might show us whether the email domain is currently in use. In that way, we can be somewhat more certain (but never completely) that someone is using that domain to send and receive email messages. This might brings us to the conclusion that the given user name is potentially valid. Let’s move to the next section to find out how we can determine this with a little extra work.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT