PHP
User Input
Sanitization
Web Development
Security

How can I sanitize user input with PHP?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

There is no single PHP function that safely "sanitizes user input" for every context. Secure input handling is a combination of validation, safe storage, parameterized database queries, and context-specific escaping at output time.

Start with Validation, Not Blind Cleaning

The first question is what the input is supposed to be. An email address, an integer, a username, and a free-form comment all need different rules. Good security starts by validating the expected shape rather than stripping characters until the value "looks safe."

php
1<?php
2$email = $_POST['email'] ?? '';
3age = $_POST['age'] ?? '';
4
5if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
6    die('Invalid email');
7}
8
9if (filter_var($age, FILTER_VALIDATE_INT) === false) {
10    die('Invalid age');
11}

Validation decides whether the value should be accepted at all. That is different from escaping or encoding.

Escape for the Output Context

If user input is going to be rendered into HTML, escape it for HTML. In PHP, htmlspecialchars is the standard tool for that output context.

php
1<?php
2$comment = $_POST['comment'] ?? '';
3
4echo htmlspecialchars($comment, ENT_QUOTES, 'UTF-8');

This protects against many cross-site scripting cases when the value is inserted into normal HTML text content.

The key point is that escaping belongs at output time, in the context where the data is used. Escaping for HTML does not make a string automatically safe for SQL, JavaScript, CSS, or shell commands.

Use Parameterized Queries for the Database

SQL injection is not solved by manually cleaning quotes out of a string. The right answer is prepared statements.

php
1<?php
2$pdo = new PDO($dsn, $user, $password, [
3    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
4]);
5
6$name = $_POST['name'] ?? '';
7
8$stmt = $pdo->prepare('INSERT INTO users (name) VALUES (:name)');
9$stmt->execute(['name' => $name]);

The database driver handles the value safely as data rather than executable SQL. That is fundamentally different from trying to sanitize SQL syntax away.

Use Filtering Functions Carefully

PHP provides sanitizing filters such as FILTER_SANITIZE_EMAIL and string-cleaning helpers such as strip_tags, but these are not universal security solutions.

php
<?php
$email = filter_var($_POST['email'] ?? '', FILTER_SANITIZE_EMAIL);

This can be useful as a preprocessing step, but you should still validate the final result. Likewise, strip_tags removes markup, but it is not a replacement for proper output escaping and it can also destroy legitimate user content.

A safer mental model is:

  • validate input when it enters the system
  • store canonical data
  • escape when the data leaves the system into a specific context

Context-Specific Examples Matter

Different contexts require different protections:

  • HTML output: htmlspecialchars
  • SQL queries: prepared statements
  • URLs: rawurlencode for components when needed
  • shell commands: avoid direct interpolation; use safe process APIs instead

Trying to pre-sanitize everything once and then reuse the same value everywhere usually creates security gaps because each output context has different rules.

Free-Form Text Should Not Be Over-Cleaned

For comments, messages, and article bodies, aggressive sanitization can remove valid content and still fail to provide real security. Usually the better design is to store the raw text, validate length and encoding, and then escape it properly when rendering.

If you genuinely allow a subset of HTML, that becomes an allowlist problem, not a simple sanitization problem. Use a trusted HTML sanitizer designed for that task rather than ad hoc regex or tag stripping.

Common Pitfalls

Looking for one universal "sanitize input" function is the biggest conceptual mistake. Input handling depends on the validation and output context.

Using htmlspecialchars before storing a value in the database mixes storage with presentation. Store the raw validated value and escape when displaying it.

Trying to prevent SQL injection with manual string cleaning instead of prepared statements is unsafe.

Summary

  • Secure PHP input handling starts with validation, not magical sanitization.
  • Escape data for the specific output context in which it is used.
  • Use prepared statements for database queries.
  • Do not confuse HTML escaping, input validation, and SQL safety; they solve different problems.

Course illustration
Course illustration

All Rights Reserved.