PHP filters are used to validate and filter data from non-secure sources, such as user input.
PHP filters are used to validate and filter data from non-secure sources.
Testing, validating, and filtering user input or custom data are important parts of any web application.
PHP's filter extensions are designed to make data filtering easier and faster.
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 your 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?
Input data from form
Cookies
Web services data
server variables
Database query results
To filter variables, use one of the following filter functions:
filter_var() - filter a single variable through a specified filter
filter_var_array() - filter multiple variables by the same or different filters
filter_input - takes an input variable and filters it
filter_input_array - takes multiple input variables and filters them through the same or different filters
In the following example, we validate an integer using the filter_var() function:
<?php $int = 123 ; if ( ! filter_var ( $int , FILTER_VALIDATE_INT ) ) { echo ( " Not a legal integer " ) ; } else { echo ( " is a legal integer " ) ; } ?>
The above code uses the "FILTER_VALIDATE_INT" filter to filter variables.
If we try to use a non-integer variable (such as "123abc"), the output will be: "Integer is not valid".
For a complete list of functions and filters, visit our PHP Filter Reference Manual.
There are two types of filters:
Validating filter:
Used to validate user input
Strict formatting rules (such as URL or E-Mail validation)
Returns the expected type if successful or FALSE if failed
Sanitizing filter:
Used to allow or prohibit specified characters in a string
No data format rules
always returns a string
Options and flags are used to add additional filtering options to the specified filter.
Different filters have different options and flags.
In the following example, we validate an integer using filter_var() with the "min_range" and "max_range" options:
<?php $var = 300 ; $int_options = array ( " options " => array ( " min_range " => 0 , " max_range " => 256 ) ) ; if ( ! filter_var ( $var , FILTER_VALIDATE_INT , $int_options ) ) { echo ( " Not a legal integer " ) ; } else { echo ( " is a legal integer " ) ; } ?>
Just like the code above, options must be put into a related array called "options". If using flags, they don't need to be in an array.
Since the integer is "300", which is not within the specified range, the output of the above code will be:
is not a legal integer
For a complete list of functions and filters, visit our PHP Filter Reference Manual. You can see the available options and flags for each filter.
Let's try to validate the input from the form.
The first thing we need to do is confirm that the input data we are looking for exists.
Then we use the filter_input() function to filter the input data.
In the following example, the input variable "email" is passed to the PHP page:
<?php if ( ! filter_has_var ( INPUT_GET , " email " ) ) { echo ( " No email parameter " ) ; } else { if ( ! filter_input ( INPUT_GET , " email " , FILTER_VALIDATE_EMAIL ) ) { echo " Not a valid email " ; } else { echo " It is a legitimate E-Mail " ; } } ?>
The example above has an input variable (email) passed via the "GET" method:
Check whether there is an "email" input variable of type "GET"
If the input variable exists, check whether it is a valid e-mail address
Let's try to clean up the URL passed from the form.
First, we want to confirm that the input data we are looking for exists.
Then, we use the filter_input() function to purify the input data.
In the following example, the input variable "url" is passed to the PHP page:
<?phpif(!filter_has_var(INPUT_GET, "url")){ echo("No url parameter");}else{ $url = filter_input(INPUT_GET, "url", FILTER_SANITIZE_URL); echo $url;}?>
The example above has an input variable (url) passed via the "GET" method:
Check if there is a "url" input variable of type "GET"
If this input variable exists, sanitize it (remove illegal characters) and store it in the $url variable
If the input variable is a string similar to this: "http://www.ruåånoøøob.com/", the sanitized $url variable will look like this:
Forms usually consist of multiple input fields. To avoid repeated calls to filter_var or filter_input function, we can use 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 GET variables received are a name, an age, and an e-mail address:
<?php $filters = array ( " name " => 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.<br> " ) ; } elseif ( ! $result [ " email " ] ) { echo ( " E-Mail is illegal<br> " ) ; } else { echo ( " Input correct " ) ; } ?>
The example above has three input variables (name, age and email) passed via the "GET" method:
Sets an array containing the names of input variables and filters for the specified input variables
Call the filter_input_array() function, the parameters include the GET input variable and the array just set
Check whether the "age" and "email" variables in the $result variable have illegal input. (If there is illegal input, the input variable is FALSE after using the filter_input_array() function.)
The second argument to the filter_input_array() function can be an array or the ID of a single filter.
If this parameter is the ID of a single filter, then the specified filter will filter all values in the input array.
If the parameter is an array, the array must follow the following rules:
Must be an associative array containing input variables that are the keys of the array (such as the "age" input variable)
The value of this array must be the ID of the filter, or an array specifying the filter, flags, and options
By using the FILTER_CALLBACK filter, you can call a custom function and use it as a filter. This way, we have full control over data filtering.
You can create your own custom function or use an existing PHP function.
Specify the filter function you intend to use in the same way as the specified option. In an associative array, with the name "options".
In the following example, we use a custom function to convert all "_" to ".":
<?php function convertSpace ( $string ) { return str_replace ( " _ " , " . " , $string ) ; } $string = " www_codercto_com! " ; echo filter_var ( $string , FILTER_CALLBACK , array ( " options " => " convertSpace " ) ) ; ?>
The above example converts all "_" to ".":
Create a function that replaces "_" with "."
Call the filter_var() function with the FILTER_CALLBACK filter and the array containing our function