Recipe 9.4. Validating Form Input: Email Addresses


9.4.1. Problem

You want to know whether an email address a user has provided is valid.

9.4.2. Solution

Use the is_valid_email_address( ) function in Example 9-10. It tells you whether an email address is valid according to the rules in RFC 822.

Validating an email address

 function is_valid_email_address($email){         $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';         $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';         $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.             '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';         $quoted_pair = '\\x5c[\\x00-\\x7f]';         $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";         $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";         $domain_ref = $atom;         $sub_domain = "($domain_ref|$domain_literal)";         $word = "($atom|$quoted_string)";         $domain = "$sub_domain(\\x2e$sub_domain)*";         $local_part = "$word(\\x2e$word)*";         $addr_spec = "$local_part\\x40$domain";         return preg_match("!^$addr_spec$!", $email) ? 1 : 0; } if (is_valid_email_address('cal@example.com')) {    print 'cal@example.com is a valid e-mail address'; } else {    print 'cal@example.com is not a valid e-mail address'; } 

9.4.3. Discussion

RFC 822 defines the standards for a valid email address. The function in Example 9-10, by Cal Henderson, uses the grammar rules laid out in that RFC to build a regular expression. You can read more about how the function is constructed at http://www.iamcal.com/publish/articles/php/parsing_email. Cal has also written a function that validates according to the more complicated rules in RFC 2822. That function is available for download from http://code.iamcal.com/php/rfc822/rfc822.phps.

The function in Example 9-10 only checks that a particular address is syntactically correct. This is useful for preventing a user from accidentally telling you that her email address is bingolover2261@example instead of bingolover2261@example.com. What it doesn't tell you, however, is what happens if you send a message to that address. Furthermore, it doesn't let you know that the person providing the email address is in control of the address. For those sorts of validations, you need to send a confirmation message to the address. The confirmation message can ask the user to take some affirmative task (reply to the message, click on a link) to indicate they're the same person that entered the address on the form. Or, the confirmation message can tell the user what to do (reply to the message, click on a link), if she's not the same person that entered the address on the form. Recipe 8.17 demonstrates a system that sends an email message containing a link that the recipient must click on to confirm that she provided the address.

9.4.4. See Also

RFC 822 at http://www.faqs.org/rfcs/rfc822.html, RFC 2822 at http://www.faqs.org/rfcs/rfc2822.html, "Parsing Email Addresses in PHP" by Cal Henderson at http://www.iamcal.com/publish/articles/php/parsing_email, and the functions available for download at http://code.iamcal.com/php/rfc822/.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net