PHP filters are used to validate and filter data coming from insecure sources, like user input.
phpsu提供的php教程
What is a PHP Filter?
A PHP filter is used to validate and filter data coming from insecure sources.
To test, validate and filter user input or custom data is an important part of any web application.
The PHP filter extension is designed to make data filtering easier and quicker. http://www.phpsu.com
Why use a Filter?
Almost all web applications depend on external input. Usually this comes from a user or another application (like a web service). By using filters you can be sure your application gets the correct input type.
welcome to phpsu.com
You should always filter all external data! phpsu.com
Input filtering is one of the most important application security issues.
phpsu is a phpschool
What is external data?
http://www.phpsu.com
- Input data from a form
- Cookies
- Web services data
- Server variables
- Database query results
Functions and Filters
To filter a variable, use one of the following filter functions:
phpsu提供的php教程
- filter_var() - Filters a single variable with a specified filter
- filter_var_array() - Filter several variables with the same or different filters
- filter_input - Get one input variable and filter it
- filter_input_array - Get several input variables and filter them with the same or different filters
In the example below, we validate an integer using the filter_var() function:
<?php if(!filter_var($int, FILTER_VALIDATE_INT)) |
The code above uses the "FILTER_VALIDATE_INT" filter to filter the variable. Since the integer is valid, the output of the code above will be: "Integer is valid".
If we try with a variable that is not an integer (like "123abc"), the output will be: "Integer is not valid".
For a complete list of functions and filters, visit our PHP Filter Reference.
www.phpsu.com
Validating and Sanitizing
There are two kinds of filters:
Validating filters:
welcome to phpsu.com
- Are used to validate user input
- Strict format rules (like URL or E-Mail validating)
- Returns the expected type on success or FALSE on failure
Sanitizing filters: phpsu
- Are used to allow or disallow specified characters in a string
- No data format rules
- Always return the string
Options and Flags
Options and flags are used to add additional filtering options to the specified filters. do you kown phpsu.com?
Different filters have different options and flags. phpsu.com
In the example below, we validate an integer using the filter_var() and the "min_range" and "max_range" options:
<?php $int_options = array( if(!filter_var($var, FILTER_VALIDATE_INT, $int_options)) |
Like the code above, options must be put in an associative array with the name "options". If a flag is used it does not need to be in an array. phpsu is a phpschool
Since the integer is "300" it is not in the specified range, and the output of the code above will be: "Integer is not valid". http://www.phpsu.com
For a complete list of functions and filters, visit our PHP Filter Reference. Check each filter to see what options and flags are available. phpsu提供的php教程
Validate Input
Let's try validating input from a form. phpsu提供的php教程
The first thing we need to do is to confirm that the input data we are looking for exists.
do you kown phpsu.com?
Then we filter the input data using the filter_input() function. phpsu.com
In the example below, the input variable "email" is sent to the PHP page: phpsu提供的php教程
<?php |
Example Explained
The example above has an input (email) sent to it using the "GET" method: www.phpsu.com
- Check if an "email" input variable of the "GET" type exist
- If the input variable exists, check if it is a valid e-mail address
Sanitize Input
Let's try cleaning up an URL sent from a form.
First we confirm that the input data we are looking for exists. welcome to phpsu.com
Then we sanitize the input data using the filter_input() function.
In the example below, the input variable "url" is sent to the PHP page: phpsu is a phpschool
<?php |
Example Explained
The example above has an input (url) sent to it using the "POST" method: phpsu.com is a free phpscool
- Check if the "url" input of the "POST" type exists
- If the input variable exists, sanitize (take away invalid characters) and store it in the $url variable
If the input variable is a string like this "http://www.W3ååSchøøools.com/", the $url variable after the sanitizing will look like this: welcome to phpsu.com
http://www.W3Schools.com/ |
Filter Multiple Inputs
A form almost always consist of more than one input field. To avoid calling the filter_var or filter_input functions over and over, we can use the filter_var_array or the filter_input_array functions.
phpsu.com is a free phpscool
In this example we use the filter_input_array() function to filter three GET variables. The received GET variables is a name, an age and an e-mail address: welcome to phpsu.com
<?php $result = filter_input_array(INPUT_GET, $filters); if (!$result["age"]) |
Example Explained
The example above has three inputs (name, age and email) sent to it using the "GET" method: phpsu.com is a free phpscool
- Set an array containing the name of input variables and the filters used on the specified input variables
- Call the filter_input_array() function with the GET input variables and the array we just set
- Check the "age" and "email" variables in the $result variable for invalid inputs. (If any of the input variables are invalid, that input variable will be FALSE after the filter_input_array() function)
The second parameter of the filter_input_array() function can be an array or a single filter ID.
If the parameter is a single filter ID all values in the input array are filtered by the specified filter. welcome to phpsu.com
If the parameter is an array it must follow these rules:
phpsu.com
- Must be an associative array containing an input variable as an array key (like the "age" input variable)
- The array value must be a filter ID or an array specifying the filter, flags and options
Using Filter Callback
It is possible to call a user defined function and use it as a filter using the FILTER_CALLBACK filter. This way, we have full control of the data filtering.
www.phpsu.com
You can create your own user defined function or use an existing PHP function
do you kown phpsu.com?
The function you wish to use to filter is specified the same way as an option is specified. In an associative array with the name "options"
phpsu
In the example below, we use a user created function to convert all "_" to whitespaces:
phpsu is a phpschool
<?php |
The result from the code above should look like this: phpsu is a phpschool
Peter is a great guy! phpsu
|
Example Explained
The example above converts all "_" to whitespaces: http://www.phpsu.com
- Create a function to replace "_" to whitespaces
- Call the filter_var() function with the FILTER_CALLBACK filter and an array containing our function
TITLE:PHP Filter