PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
235: Building a Newsletter Subscription Service
By now, you're comfortable with databases and form handling, so building a newsletter subscription service feels like a weekend project. It seems simple: you need an input field for an email address, a table to store it, and a way to send a "Welcome!" email. If you're in a rush, you might be tempted to just glue these three things together in a single PHP script.
The temptation of the "Just Save It" script
In the naive version of this service, you'd likely write a script that accepts a POST request, runs an INSERT statement, and then calls the mail() function right there in the middle of the execution. It looks clean on paper. You hit submit, the database updates, the email sends, and you redirect the user to a "Thank You" page. I've seen this in countless junior projects, and it works perfectly... until you actually have users.
// The naive way: Don't do this in production
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'];
$db->query("INSERT INTO subscribers (email) VALUES ('$email')");
mail($email, "Welcome!", "Thanks for joining our newsletter!");
header("Location: /thanks.php");
}
The first thing that breaks here isn't even the code; it's the user experience. The mail() function is synchronous. Your PHP script pauses everything while it talks to the mail server. If the SMTP server is sluggish or the network hiccups, your user is staring at a white screen for five or ten seconds. In the modern web, a five-second delay feels like an eternity. You're essentially holding the user's browser hostage while your server does chores.
Decoupling the subscription from the notification
To fix the latency issue, we have to stop treating the email as part of the request. Instead of sending the mail immediately, I want you to think of the database as a "to-do list." When a user signs up, you save their email and a status flag—something like is_processed = 0. Then, you immediately redirect the user to the thank-you page. The user is happy because the page loaded instantly.
Now, you have a separate PHP script—a worker—that runs in the background (perhaps via a cron job every minute). This script looks for any rows where is_processed is 0, sends the emails, and flips the flag to 1. This way, if your mail server goes down for an hour, your website doesn't crash; the emails just queue up and send once the service is back online. It's a much more resilient architecture.
The risk of "Single Opt-In"
There's another problem with the naive approach: trust. If you just take an email and add it to your list, you're inviting trouble. Someone could write a simple script to submit a thousand fake email addresses to your form. Suddenly, your mail server is sending spam to people who never signed up, and your IP address gets blacklisted by Gmail and Outlook. Once you're blacklisted, even your legitimate business emails stop arriving.
The professional way to handle this is "Double Opt-In." Instead of adding them to the active subscriber list immediately, you create a pending_subscriptions table. You generate a long, random token—something like bin2hex(random_bytes(32))—and save it alongside the email. You send a confirmation email with a link: subscribe.php?token=abc123xyz.
Only when the user clicks that link do you actually move them into your main subscribers table. It's an extra step for the user, sure, but it guarantees that the email address is valid and that the owner actually wants your content. It protects your sender reputation and keeps your list clean of bots.
📋 Practical Task
Implementing the Double Opt-In Token System
Build a subscription flow that prevents immediate activation. You will need to create two tables: pending_subscriptions (storing email, token, and created_at) and subscribers (storing email and subscribed_at).
- Create a
signup.phpscript that generates a secure random token, stores the email in the pending table, and simulates sending an email (you can justechothe confirmation link to the screen for testing). - Create a
verify.phpscript that accepts atokenvia GET. It must check if the token exists in the pending table, move that email address to thesubscriberstable, and then delete the record from the pending table to prevent the token from being used twice. - Ensure you use prepared statements for all database interactions to prevent SQL injection.
There are no comments for now.