PHP filter

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

What are PHP filters?

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 extension is designed to make data filtering easier and faster.

Why use filters?

Nearly 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?

1. Input data from forms

2. Cookies

3. Web services data

4. Server variables

5 . Database query results

Functions and filters

To filter variables, use one of the following filter functions:

• filter_var() - Filter a single variable by a specified filter

• filter_var_array() - Filter multiple variables by the same or different filters

• filter_input - Get an input variable and filter it

• filter_input_array - Get multiple input variables and filter them by the same or different filters

In the example below , we use the filter_var() function to verify an integer:

<?php
$int = 123;
if(!filter_var($int, FILTER_VALIDATE_INT))
{
echo("不是一个合法的整数");
}
else
{
echo("是个合法的整数");
}
?>

The above code uses the "FILTER_VALIDATE_INT" filter to filter variables. Since this integer is legal, the above code will output:

is a legal integer

If we try to use a non-integer variable (such as "123abc"), it will output: "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:

• Used to validate user input

• Strict formatting rules (such as URL or E-Mail validation)

• Returns the expected value if successful Type, returns FALSE if failed

Sanitizing filter:

• Used to allow or prohibit specified characters in the string

• No data format rules

• Always returns the 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 example below, 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("不是一个合法的整数");
}
else
{
echo("是个合法的整数");
}
?>

Just like the code above, the option must be placed in an options" in the associated array. 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.


Validating input

Let us 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("没有 email 参数");
}
else
{
if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL))
{
echo "不是一个合法的 E-Mail";
}
else
{
echo "是一个合法的 E-Mail";
}
}
?>

The test results of the above example are as follows:

103.jpgExplanation of examples

The above example has an input variable (email) transmitted through the "GET" method:

Detect whether there is an "email" of type "GET" Input variables

If an input variable exists, check if it is a valid e-mail address


Purify input

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

First, we need 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:

<?php
if(!filter_has_var(INPUT_GET, "url"))
{
echo("没有 url 参数");
}
else
{
$url = filter_input(INPUT_GET, 
"url", FILTER_SANITIZE_URL);
echo $url;
}
?>

Explanation of the example

The above example has a pass Input variable (url) transmitted by the "GET" method:

Detect whether there is an "url" input variable of "GET" type

If this input variable exists, purify it (delete 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 purified $url variable is as follows Shown:

104.jpg


Filter multiple inputs

form Usually consists of multiple input fields. To avoid repeated calls to filter_var or filter_input functions, 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 received GET variables 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("年龄必须在 1 到 120 之间。<br>");
}
elseif(!$result["email"])
{
echo("E-Mail 不合法<br>");
}
else
{
echo("输入正确");
}
?>

Explanation

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

1. Set up an array that contains the name of the input variable and the specified input Variable filter

2. Call the filter_input_array() function, the parameters include the GET input variable and the array just set

3. Detect the "age" and "email" variables in the $result variable Is there any illegal input? (If there is an 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, then the array must follow the following rules:

Must be an associative array, the input variable contained in it is the key 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

Use Filter Callback

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 function of the filter you are going to use according to the specified method of the specified option. In an associative array, with the name "options".

In the following example, we use a custom function to convert all "_" to spaces:

<?php
function convertSpace($string)
{
return str_replace("_", ".", $string);
}
$string = "www_php_com!";
echo filter_var($string, FILTER_CALLBACK,
array("options"=>"convertSpace"));
?>

The result of the above code is as follows:

www.php.com

Explanation of examples

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

1. Create a function that replaces "_" with "."

2. Call the filter_var() function, whose parameters are the FILTER_CALLBACK filter and the array containing our function


Continuing Learning
||
<?php $int = 123; if(!filter_var($int, FILTER_VALIDATE_INT)) { echo("不是一个合法的整数"); } else { echo("是个合法的整数"); } ?>
submitReset Code