Chapter practice task
Task: Build a secure contact form that emails you and shows a thank-you message
- Build a form with name, email, service and message fields.
- Validate every field on the server and show friendly errors next to each field.
- Keep the visitor’s input in the form when there are errors.
- Protect the form with a CSRF token stored in a session and a honeypot field.
- Send yourself an email with a Reply-To header and redirect to a thank-you message.
By the end of this chapter
- You can build a complete form handler from scratch
- You validate on the server and escape every value you print
- You can send email from PHP and know when to use SMTP instead
- You can store data across pages with sessions and use them for flash messages and CSRF protection
Quick answers for this chapter
Isn’t HTML5 validation enough?
No. Visitors and bots can skip the browser completely. Browser validation is for convenience; server validation is for safety.
Why do my PHP emails go to spam?
Usually because the server isn’t authorized to send for your domain. Sending through SMTP with SPF, DKIM and DMARC set up fixes most cases.
Where is session data stored?
On the server. The visitor’s browser only keeps a random session ID in a cookie.