Validating and Sanitizing Input

Tutorial 4 of 5

1. Introduction

In this tutorial, we're going to learn about validating and sanitizing user input in PHP. Understanding these concepts is crucial because it helps maintain data integrity and prevent security vulnerabilities like SQL Injection and Cross-Site Scripting (XSS) attacks.

By the end of this tutorial, you'll be able to:

  • Understand the importance of validating and sanitizing user input.
  • Use PHP's built-in functions to validate and sanitize the input.

The prerequisite for this tutorial is a basic understanding of PHP.

2. Step-by-Step Guide

2.1 Input Validation

Input validation is the process of checking if the data provided by the user meets certain criteria before processing it.

$email = $_POST["email"];

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo("Invalid email format");
}

In this example, filter_var function is used to validate the email. If the email is not valid, it echoes "Invalid email format".

2.2 Input Sanitization

Input sanitization is the process of cleaning and formatting the user input data.

$name = $_POST["name"];
$name = filter_var($name, FILTER_SANITIZE_STRING);

Here, filter_var function is used with FILTER_SANITIZE_STRING filter. It will remove all HTML tags from the string.

3. Code Examples

Example 1: Validating an integer

$age = $_POST["age"];

if (!filter_var($age, FILTER_VALIDATE_INT)) {
  echo("Invalid age");
} else {
  echo("Valid age");
}

In this example, we use FILTER_VALIDATE_INT to validate if the input is an integer. If not, it will echo "Invalid age"; otherwise, it will echo "Valid age".

Example 2: Sanitizing a URL

$url = $_POST["url"];
$url = filter_var($url, FILTER_SANITIZE_URL);

if (!filter_var($url, FILTER_VALIDATE_URL)) {
  echo("Invalid URL");
} else {
  echo("Valid URL");
}

In this example, we sanitize the URL input using FILTER_SANITIZE_URL and then validate it using FILTER_VALIDATE_URL.

4. Summary

In this tutorial, we learned about the importance of validating and sanitizing user input to maintain data integrity and prevent security vulnerabilities. We also looked at how to use PHP's built-in functions to accomplish these tasks.

For further learning, you can look into other types of filters available in PHP and how to create your own custom validation rules.

5. Practice Exercises

Exercise 1:

Write a PHP script to validate and sanitize a user input email.

Solution:

$email = $_POST["email"];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo("Invalid email");
} else {
  echo("Valid email");
}

In this solution, we first sanitize the email input using FILTER_SANITIZE_EMAIL and then validate it using FILTER_VALIDATE_EMAIL.

Exercise 2:

Write a PHP script to validate and sanitize a user input integer.

Solution:

$age = $_POST["age"];
$age = filter_var($age, FILTER_SANITIZE_NUMBER_INT);

if (!filter_var($age, FILTER_VALIDATE_INT)) {
  echo("Invalid age");
} else {
  echo("Valid age");
}

Here, we first sanitize the age input using FILTER_SANITIZE_NUMBER_INT and then validate it using FILTER_VALIDATE_INT.

For further practice, you can try validating and sanitizing other types of data, like URLs, IP addresses, and more.