PHP Forms
Validating Form Input: Email Addresses
Problem
You want to know whether an email address a user has provided is valid.Solution
Example Validating an email address$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($email === false) {
print "Submitted email address is invalid.";
}
Discussion
RFC 5321 consolidates a number of email-related RFCs and defines the standards for a valid email address. The FILTER_VALIDATE_EMAIL filter uses a regular expression based on those rules, although it explicitly does not support comments or folding whitespace.
The filter 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 if she’s not the same person that entered the address on the form — such as to click on a link in the messsage to indicate the wrong address was entered. Demonstrates a system that sends an email message containing a link that the recipient must click on to confirm that she provided the address.
No comments:
Post a Comment