1. Code
  2. PHP

Sanitize and Validate Data With PHP Filters

Scroll to top

Data validation is an integral part of working with forms. Not only can invalid submitted data lead to security problems, but it can also break your webpage. Today, we'll take a look at how to remove illegal characters and validate data by using the filter_var function.

Importance of Data Sanitization

Data sanitization is important to prevent security vulnerabilities such as SQL injection, cross-site scripting (XSS), and other malicious attacks. It ensures that user input data is cleaned and validated before being processed by the application.

Here are a couple of reasons why you must opt for data sanitization in PHP:

Prevents SQL Injection

One of the most significant security threats to web applications is SQL injection. Malicious users use SQL injection to gain unauthorized access to the database or manipulate data. By sanitizing user input data, developers can prevent such SQL injection attacks.

Protection Against XSS (Cross-Site Scripting) Attacks

Cross-site scripting (XSS) is another major security vulnerability in web applications. These kinds of attacks allow hackers to easily inject malicious scripts into webpages and steal sensitive user data such as cookies. By sanitizing user input data, developers can prevent XSS attacks.

Data Sanitization

Data sanitization ensures that the data entered by users is accurate and consistent. It helps to remove any unwanted characters or tags that may cause errors and lead to inconsistent data.

Example Without Data Sanitization

Let's quickly look at an example with malicious user input.

Suppose we have a login form on a website where users can enter their username and password to access their account. The PHP code might look something like this:

1
...
2
...
3
// Check if the username and password are valid

4
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
5
$result = mysqli_query($conn, $sql);
6
...
7
...

In the above code, the user's input is used to prepare an SQL query which checks if the username and password entered by the user match a valid user in the database.

However, if the hacker enters a malicious input like this:

1
' OR '1'='1

The resulting SQL statement would be like this:

1
SELECT * FROM users WHERE username='' OR '1'='1' AND password='';

As you can see, the query will always evaluate to TRUE, which means that the SQL query will return all users in the database, effectively giving the hacker access to all user accounts.

This is just one example of how malicious input can compromise the security of your application. Thus, it's really important to sanitize and validate users' input to prevent such attacks.

Why Developers Tend to Skip Data Sanitization

Most people tend to think of data validation as an immensely tedious process where one:

  • compares the data they want to validate against every possible combination they can think of
  • tries to find a golden regular expression that will match every possible combination
  • or a combination of the two

There are obvious problems with the above:

  • it's very time-consuming
  • there is a very high chance of error

Fortunately, with the latest versions of PHP, there's a function called filter_var which takes away the pain of data validation. 

Syntax of the filter_var Function

In PHP, the filter_var() function is a really powerful tool for sanitizing and validating user input. It is used to filter a variable with a specified filter, which can be one of the many predefined filters. In fact, you could use a custom filter as well, with the help of a callback function.

Let's quickly go through the syntax of the filter_var function.

1
filter_var( mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0 ): mixed
  • $variable: the variable which you want to filter.
  • $filter: the ID of the filter which you want to apply to the $variable. It can be any of the predefined filters that are available in PHP, or your custom filter.
  • $options: an optional parameter which is used to specify additional options for the filter which is being applied. It depends on the specific filter which you're using.

The filter_var() function returns the filtered value if the filter is successful, or FALSE if the filter fails.

How to Sanitize Using the filter_var Function

In this section, we'll discuss how you can sanitize data using the filter_var function.

How to Sanitize the Email

The FILTER_SANITIZE_EMAIL filter is used to remove any illegal characters from an email address. It's useful for sanitizing user input before it is stored or displayed.

Let's have a look at the following example:

1
$email = "test@example.com<script>alert('hello')</script>";
2
$sanitizedEmail = filter_var($email, FILTER_SANITIZE_EMAIL);

In the above example, the $email variable contains an email address with a malicious script injected into it. The filter_var() function is used with the FILTER_SANITIZE_EMAIL filter to remove any illegal characters from the email address. Thus, the resulting sanitized email address is stored in the $sanitizedEmail variable. If you try to print it using echo, the script tag and its contents should have been removed, leaving only the valid email address.

How to Sanitize the URL

The FILTER_SANITIZE_URL filter is used to remove any illegal characters from a URL. This filter is useful for sanitizing user input before it is stored or displayed.

Let's have a look at the following example:

1
$url = "https://www.example.com/?q=<script>alert('hello')</script>";
2
$sanitizedUrl = filter_var($url, FILTER_SANITIZE_URL);

In this example, the $url variable contains a URL with a malicious script injected into the query string. The filter_var() function is used with the FILTER_SANITIZE_URL filter to remove any illegal characters from the URL. The resulting sanitized URL is then stored in the $sanitizedUrl variable. When the sanitized URL is printed using echo, the script tag and its contents should have been removed.

How to Validate Using the filter_var Function

In this section, we'll discuss how you can validate data using the filter_var function.

How to Validate the IP Address

Let's have a look at the following example.

1
$ip = "127.0.0.1";
2
3
if (filter_var($ip, FILTER_VALIDATE_IP)) {
4
  // Valid IP address

5
} else {
6
  // Invalid IP address

7
}

In the above example, the FILTER_VALIDATE_IP filter is used to check if the $ip variable contains a valid IP address.

How to Validate the Integer

Let's have a look at the following example.

1
$foo = "123";
2
3
if (filter_var($foo, FILTER_VALIDATE_INT)) {
4
  // Valid integer

5
} else {
6
  // Invalid integer

7
}

In the above example, the FILTER_VALIDATE_INT filter is used to check if the $foo variable contains a valid integer.

Putting It All Together: An Email Submit Form

After covering the concepts of data sanitation and validation, we will use these skills to create a simple email submission form. Although the form will not be of production quality, as an example it should be sufficient for this tutorial. The form will require four pieces of information:

  • name
  • email address
  • home page
  • message

We will sanitize and validate all four pieces of data and send the email only if all of them are valid. If anything is invalid or any field is left blank, we will present the form to the user with a list of items that need to be fixed. We will also return the sanitized data to the user in case they are unaware of which characters are illegal.

Step 1. Creating the Form

For the first step, simply create a form element with five fields: the four listed above and a submit button.

1
<form name="form1" method="post" action="form-email.php">
2
    Name: <br/>
3
    <input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="50" /><br/><br/>
4
    Email Address: <br/>
5
    <input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/> <br/><br/>
6
    Home Page: <br/>
7
    <input type="text" name="homepage" value="<?php echo $_POST['homepage']; ?>" size="50" /> <br/><br/>
8
    Message: <br/>
9
    <textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>
10
    <br/>
11
    <input type="submit" name="Submit" />
12
</form>

Step 2. Handle the Form Submission

When the form is submitted, the data is sent to form-email.php file.

Let's create the form-email.php file with the following contents.

1
<?php
2
    if (isset($_POST['Submit'])) {
3
4
        if ($_POST['name'] != "") {
5
            $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
6
            if ($_POST['name'] == "") {
7
                $errors .= 'Please enter a valid name.<br/><br/>';
8
            }
9
        } else {
10
            $errors .= 'Please enter your name.<br/>';
11
        }
12
13
        if ($_POST['email'] != "") {
14
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
15
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
16
                $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
17
            }
18
        } else {
19
            $errors .= 'Please enter your email address.<br/>';
20
        }
21
22
        if ($_POST['homepage'] != "") {
23
            $homepage = filter_var($_POST['homepage'], FILTER_SANITIZE_URL);
24
            if (!filter_var($homepage, FILTER_VALIDATE_URL)) {
25
                $errors .= "$homepage is <strong>NOT</strong> a valid URL.<br/><br/>";
26
            }
27
        } else {
28
            $errors .= 'Please enter your home page.<br/>';
29
        }
30
31
        if ($_POST['message'] != "") {
32
            $_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
33
            if ($_POST['message'] == "") {
34
                $errors .= 'Please enter a message to send.<br/>';
35
            }
36
        } else {
37
            $errors .= 'Please enter a message to send.<br/>';
38
        }
39

First, we check if the form has been submitted using the isset() function. If the form has been submitted, the validation and sanitation code is executed. We check each form field to see if it has been set, and if it has, we sanitize it with the appropriate filter functions from filter_var(). If the user input is invalid or empty, the corresponding error message is added to the $errors variable.

1
        if (!$errors) {
2
            $mail_to = 'me@somewhere.com';
3
            $subject = 'New Mail from Form Submission';
4
            $message  = 'From: ' . $_POST['name'] . "\n";
5
            $message .= 'Email: ' . $_POST['email'] . "\n";
6
            $message .= 'Homepage: ' . $_POST['homepage'] . "\n";
7
            $message .= "Message:\n" . $_POST['message'] . "\n\n";
8
            mail($to, $subject, $message);
9
            echo "Thank you for your email!<br/><br/>";
10
        } else {
11
            echo '<div style="color: red">' . $errors . '<br/></div>';
12
        }
13
    }
14
?>

If there are no errors, an email is sent using the mail() function with the sanitized user input data. Finally, the code displays a thank-you message if the email is sent successfully, otherwise it displays the error message(s) if there were any errors.

Conclusion

I hope that this tutorial has given you a good introduction to PHP's data filtering features. Although we haven't covered all the rules and functions, you can find more information in the Data Filtering section of the PHP manual.

The thumbnail for this post was generated with OpenAI's DALL-E 2.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.