PHP filters are used to validate and filter data from unsecured sources, such as user input.
What is a PHP filter?
PHP filters are used to validate and filter data from unsecured sources.
Validating and filtering user input or custom data is an important part of any WEB application.
The purpose of designing a PHP filter extension is to make data filtering easier and faster.
Why use filters?
Almost all Web applications rely on external input. This data typically comes from users or other applications (such as Web services). By using a filter, you can ensure that your program gets the correct input type.
You should always filter the external data!
Input filtering is one of the most important application security topics.
What is external data?
Input data from a form
Cookies
Server variables
Database Query Results
Functions and filters
To filter variables, use one of the following filter functions:
Filter_var ()-Filters a single variable with a specified filter
Filter_var_array ()-Filter multiple variables through the same or different filters
Filter_input-Get an input variable and filter it
Filter_input_array-gets multiple input variables and filters them through the same or different filters
In the following example, we use the Filter_var () function to validate an integer:
Copy Code code as follows:
<?php
$int = 123;
if (!filter_var ($int, Filter_validate_int))
{
Echo ("Integer is not valid");
}
Else
{
Echo ("Integer is valid");
}
?>
The code above uses the "Filter_validate_int" filter to filter variables. Because this integer is legitimate, the output of the code is: "The integer is valid".
If we try to use a variable with a non integer, the output is: "The Integer is not valid".
For a complete list of functions and filters, please visit our PHP Filter reference manual.
Validating and sanitizing
There are two types of filters:
Validating filter:
For validating user input
Strict formatting rules (such as URL or e-mail validation)
Returns FALSE if the expected type of success is returned
Sanitizing Filter:
Used to allow or disallow characters specified in a string
Countless formatting rules
Always return string
Options and flags
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 validated an integer with the Filter_var () and the "Min_range" and "Max_range" options:
Copy Code code as follows:
<?php
$var = 300;
$int _options = Array (
"Options" =>array
(
"Min_range" =>0,
"Max_range" =>256
)
);
if (!filter_var ($var, Filter_validate_int, $int _options))
{
Echo ("Integer is not valid");
}
Else
{
Echo ("Integer is valid");
}
?>
Like the code above, the option must be placed in an associated array named "Options." If you use a flag, you do not need to be inside the array.
Since the integer is "300", it is not in the specified atmosphere, the output of the above code will be "integer is not valid".
For a complete list of functions and filters, please visit the PHP Filter reference manual provided by W3school. You can see the available options and flags for each filter.
Validating input
Let's try validating the input from the form.
The first thing we need to do is confirm that we have the input data we are looking for.
Then we use the Filter_input () function to filter the input data.
In the following example, the input variable "email" is uploaded to the PHP page:
Copy Code code as follows:
<?php
if (!filter_has_var (input_get, "email"))
{
Echo ("Input type does not exist");
}
Else
{
if (!filter_input (input_get, "email", filter_validate_email))
{
echo "e-mail is not valid";
}
Else
{
echo "e-mail is valid";
}
}
?>
Example Explanation:
The above example has an input variable (email) that passes through the "Get" method:
Detect the existence of "get" type of "email" input variable
If an input variable exists, detect if it is a valid mail address
Purifying input
Let's try to clean up the URLs that came from the form.
First, we want to make sure we have the input data we're looking for.
We then use the Filter_input () function to purify the input data.
In the following example, the input variable "url" is uploaded to the PHP page:
Copy Code code as follows:
<?php
if (!filter_has_var (input_post, "url"))
{
Echo ("Input type does not exist");
}
Else
{
$url = Filter_input (Input_post,
"url", Filter_sanitize_url);
}
?>
Example Explanation:
The example above has an input variable (URL) that is routed through the "POST" method:
Detect if there is a "POST" type of "url" input variable
If the input variable exists, purify it (remove the illegal characters) and store it in the $url variable
If the input variable is like this: "http://www.w3#$%s^%$ #ool. com.cn/", then the purified $url variable should be:
http://www.W3School.com.cn/Filter Multiple Inputs
A form is usually made up of multiple input fields. To avoid repeated calls to Filter_var or filter_input, we can use the Filter_var_array or the Filter_input_array function.
In this case, we use the Filter_input_array () function to filter three get variables. The receive variable received is a name, an age, and a mail address:
Copy Code code as follows:
<?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 to be a number between 1 and 120.<br/>");
}
ElseIf (! $result ["email"])
{
Echo ("e-mail is not valid.<br/>");
}
Else
{
Echo ("User input is valid");
}
?>
Example Explanation:
The above example has three input variables passed through the "Get" method (name, age and email)
Sets an array that contains the name of the input variable and the filter used for the specified input variable
Call the Filter_input_array function, which includes the get input variable and the array you just set
Detects whether the "age" and "email" variables in the $result variable have illegal input. (if there is an illegal input,)
The second parameter of the Filter_input_array () function can be the ID of an array or a single filter.
If the parameter is the ID of a single filter, the specified filter filters all the values in the input array.
If the argument is an array, the array must follow the following rules:
Must be an associative array containing the input variables that are the keys to the array (such as the "age" input variable)
The value of this array must be the ID of the filter, or an array of filters, flags, and options
Using the Filter Callback
By using the Filter_callback filter, you can call a custom function and use it as a filter. In this way, we have full control over the data filtering.
You can create your own custom functions, or you can use existing PHP functions.
A function that requires you to use a filter in the same way that you would specify an option.
In the following example, we use a custom function to convert all "_" to Spaces:
Copy Code code as follows:
<?php
function Convertspace ($string)
{
Return Str_replace ("_", "", $string);
}
$string = "peter_is_a_great_guy!";
Echo Filter_var ($string, Filter_callback,
Array ("Options" => "Convertspace"));
?>
The result of the above code is this:
Peter is a great guy! example explains:
The above example converts all "_" to Spaces:
Create a function that replaces "_" with a space
Call the Filter_var () function, whose argument is the Filter_callback filter and the array containing our functions
Current 1/2 page
12 Next read the full text