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
24: Handling Forms and User Input
A few years ago, I was reviewing a PR for a junior developer who had built a simple "Contact Us" form for a client. It looked great and it worked perfectly during his local tests. The problem started the moment it went live. A prankster discovered that the "Name" field wasn't being sanitized and submitted a string of HTML and JavaScript. Every time the client opened their admin dashboard to read the messages, the page would trigger an alert box that said "Hacked!" and then redirect the browser to a YouTube video of a dancing hamster. It wasn't a sophisticated attack, but it was a loud reminder: user input is radioactive. You never, ever trust it.
Choosing Between POST and GET
When you're building a form, your first decision is how to send that data to the server. In PHP, you'll mostly deal with $_GET and $_POST. I like to think of $_GET as "bookmarks." Since the data is appended directly to the URL (like search.php?query=php+tutorials), it's great for search pages or filters. If a user refreshes the page, the data stays there. However, it's terrible for passwords or large amounts of text because URLs have length limits and the data is visible to anyone looking at the browser history.
For everything else—especially anything that changes data in your database, like creating a support ticket or updating a profile—use $_POST. The data is sent in the HTTP request body, making it invisible to the casual observer and capable of handling much larger payloads. Just remember: if you use $_POST and the user hits "Refresh" after submitting, the browser will pop up that annoying "Confirm Form Resubmission" warning. That's a feature, not a bug; it prevents the user from accidentally buying the same product twice.
Cleaning the Mess with Validation and Sanitization
Now, back to the "dancing hamster" problem. To stop users from injecting scripts or breaking your database, you need two different tools: validation and sanitization. I see people confuse these all the time, but they are distinct steps. Validation is asking, "Is this data what I expected?" (e.g., Is this actually an email address?). Sanitization is saying, "I don't care what this is, I'm going to strip out the dangerous parts."
In modern PHP, filter_input() is your best friend. It combines these two concepts beautifully. Instead of accessing $_POST['email'] directly, which can trigger an "Undefined index" notice if the field is missing, use filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL). It returns the cleaned value if it's valid, and false if it's not. It's cleaner, safer, and handles the existence check for you.
// The wrong way: Trusting the user
$email = $_POST['email'];
// The right way: Validating and cleaning
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if (!$email) {
echo "Please provide a valid email address.";
}
Preventing XSS on the Way Out
Here is the golden rule: sanitize on the way in, but escape on the way out. Even if you've cleaned the data before putting it in a database, you should still treat it as dangerous when you echo it back to the browser. This is where htmlspecialchars() comes in. It converts characters like < and > into HTML entities (< and >), meaning the browser will display the code as text rather than executing it as a script.
I've seen developers skip this because they "already sanitized the input." Don't be that person. Different contexts require different escaping. What's safe for a database might not be safe for an HTML page. If you wrap your output in htmlspecialchars(), you effectively kill any XSS (Cross-Site Scripting) attempts in their tracks.
// Even if $username came from a "safe" database, escape it here
echo "Welcome back, " . htmlspecialchars($username, ENT_QUOTES, 'UTF-8');📋 Practical Task
Build a Secure Support Ticket Submission System
Your task is to create a single-page PHP script that handles a support ticket form. You need to ensure the application is resilient against malicious input and handles errors gracefully.
Requirements:
- Create an HTML form that collects a Username, an Email Address, and a Priority Level (use a dropdown with options: Low, Medium, High). The form must use the
POSTmethod. - Use
filter_input()to retrieve and validate the email address. If the email is invalid or the username is empty, display a specific error message to the user. - Ensure that the "Priority Level" is validated against an array of allowed values (Low, Medium, High) to prevent users from manipulating the HTML to send an unsupported priority.
- If the input is valid, display a "Success" message that echoes the username and priority level back to the user, ensuring you use
htmlspecialchars()to prevent any XSS attacks.
There are no comments for now.