Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP Filters

PHP Filters


PHP filters are used to validate and filter data from untrusted sources, such as user input.


What are PHP Filters?

PHP filters are used to validate and filter data from untrusted sources.

Testing, validating, and filtering user input or custom data is an important part of any web application.

The design purpose of PHP's filter extension is to make data filtering easier and faster.


Why Use Filters?

Almost all web applications rely on external input. This data usually comes from users or other applications (such as web services). By using filters, you can ensure that the application gets the correct input type.

You should always filter external data!

Input filtering is one of the most important application security topics.

What is External Data?


Functions and Filters

To filter a variable, use one of the following filter functions:

In the example below, we validated an integer using the filter_var() function:

Example

The code above used the "FILTER_VALIDATE_INT" filter to filter the variable. Since the integer is valid, the code above will output:

If we try to use a non-integer variable (such as "123abc"), it will output: "Integer is not valid".

To view the complete list of functions and filters, please visit our PHP Filter Reference Manual.


Validating and Sanitizing

There are two types of filters:

Validating filters:

Sanitizing filters:


Options and Flags

Options and flags are used to add extra filtering options to a specified filter.

Different filters have different options and flags.

In the example below, we validated an integer using filter_var() with the "min_range" and "max_range" options:

Example

array ( " min_range " => 0 , " max_range " => 256 ) ) ; if ( ! filter_var ( $var , FILTER_VALIDATE_INT , $int_options ) ) { echo ( " is not a valid integer " ) ; } else { echo ( " is a valid integer " ) ; } ?>

Just like the code above, options must be placed in a related array named "options". If flags are used, they need not be inside the array.

Since the integer is "300", which is not within the specified range, the output of the above code will be:

不是一个合法的整数

To view the complete list of functions and filters, please visit our PHP Filter Reference Manual. You can see the available options and flags for each filter.


Validating Input

Let's try to validate input from a form.

The first thing we need to do is confirm whether the input data we are looking for exists.

Then we use the filter_input() function to filter the input data.

In the example below, the input variable "email" is passed to the PHP page:

Example

The test result of the above example is as follows:

Example Explanation

The example above has an input variable (email) passed via the "GET" method:

  1. Check whether a "GET" type "email" input variable exists
  2. If the input variable exists, check whether it is a valid e-mail address

Sanitizing Input

Let's try to clean up the URL passed from the form.

First, we need to confirm whether the input data we are looking for exists.

Then, we use the filter_input() function to sanitize the input data.

In the example below, the input variable "url" is passed to the PHP page:



Example Explanation

The example above has an input variable (url) passed via the "GET" method:

  1. Check whether the "url" input variable of type "GET" exists
  2. If this input variable exists, sanitize it (remove illegal characters) and store it in the $url variable

If the input variable is a string like this: "http://www.ruåånoøøob.com/", then the sanitized $url variable looks like this:


Filtering multiple inputs

Forms usually consist of multiple input fields. To avoid repeatedly calling the filter_var or filter_input functions, we can use the filter_var_array or the filter_input_array function.

In this example, we use the filter_input_array() function to filter three GET variables. The received GET variables are a name, an age, and an e-mail address:

Example

array ( " filter " => FILTER_SANITIZE_STRING ) , " age " => array ( " filter " => FILTER_VALIDATE_INT , " options " => array ( " min_range " => 1 , " max_range " => 120 ) ) , " email " => FILTER_VALIDATE_EMAIL ) ; $result = filter_input_array ( INPUT_GET , $filters ) ; if ( ! $result [ " age " ] ) { echo ( " Age must be between 1 and 120.
" ) ; } elseif ( ! $result [ " email " ] ) { echo ( " E-Mail is invalid
" ) ; } else { echo ( " Input is correct " ) ; } ?>

Example Explanation

The example above has three input variables (name, age, and email) passed via the "GET" method:

  1. Set up an array containing the names of the input variables and the filters to be applied to the specified input variables
  2. Call the filter_input_array() function with the GET input variables and the array set up above as parameters
  3. Check whether the "age" and "email" variables in the $result variable contain illegal input. (If illegal input exists, after using the filter_input_array() function, the input variable becomes FALSE.)

The second parameter of the filter_input_array() function can be an array or a single filter ID.

If this parameter is a single filter ID, then this specified filter will filter all values in the input array.

If this parameter is an array, then this array must follow the rules below:


Using Filter Callback

By using the FILTER_CALLBACK filter, you can invoke a custom function and use it as a filter. In this way, we gain full control over data filtering.

You can create your own custom function, or use an existing PHP function.

Specify the function you intend to use as the filter according to the method specified by the options. In the associative array, it has the name "options".

In the example below, we use a custom function to convert all "_" into ".":

Example

" convertSpace " ) ) ; ?>

The result of the code above is shown below:

Example Explanation

The example above converts all "_" into ".":

  1. Create a function that replaces "_" with "."
  2. Call the filter_var() function, with the FILTER_CALLBACK filter and an array containing our function as parameters