12.1. Email Validation

Validating a user's email address helps you avoid basic spam attempts. Some spam bots simply add garbage text into a form's inputs. By ensuring that the email input is filled out with a properly formatted email address, you can fend off the most primitive types of spam attacks.

However, you also want to keep in mind that people sometimes make typos, and if your comment form can't validate a user with a typo in her email address, you don't want to erase her comment completely and make her start over; this will frustrate your user and might prevent this person from joining the discussion on your blog.

To avoid this problem, you can save the comment in a session that you destroy only if the comment is posted successfully ...

Get PHP for Absolute Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.