PHP filter implementation page 1/2

Source: Internet
Author: User
Tags valid email address
To implement a filter-like function in PHP, because I need this, a developed OA system should be integrated into our applications, OA needs to filter external data and process it for itself. The PHP filter is used to verify and filter data from non-secure sources, such as user input.
What is a PHP filter?
PHP filters are used to verify and filter data from unsafe sources.

Verifying and filtering 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 faster.
Why filter?
Almost all web applications depend on external input. This data usually comes from users or other applications (such as web services ). By using a filter, you can ensure that the program obtains the correct input type.

You should always filter external data!

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

What is external data?
Input data from form
Cookies
Server variables
Database query results
Functions and filters
To filter variables, use one of the following filter functions:

Filter_var ()-filter a single variable using a specified filter
Filter_var_array ()-filter multiple variables using the same or different filters
Filter_input-get an input variable and filter it
Filter_input_array-obtain multiple input variables and filter them using the same or different filters.
In the following example, we use the filter_var () function to verify an integer:

The code is as follows:


$ Int = 123;

If (! Filter_var ($ int, FILTER_VALIDATE_INT ))
{
Echo ("Integer is not valid ");
}
Else
{
Echo ("Integer is valid ");
}
?>


The above code uses the "FILTER_VALIDATE_INT" filter to filter variables. Because this Integer is valid, the output of the code is: "Integer is valid ".

If we try to use a non-Integer variable, the output is: "Integer is not valid ".

For a complete list of functions and filters, visit our PHP Filter reference manual.
Validating and Sanitizing
There are two filters:

Validating filter:
Used to verify user input
Strict format rules (such as URL or email verification)
Returns the expected success type. otherwise, FALSE is returned.
Sanitizing filter:
Allows or disables specified characters in a string.
No data format rules
Always returns a string
Options and flag
The options and flag are used to add additional filter options to the specified filter.

Different filters have different options and logos.

In the following example, we use the filter_var (), "min_range", and "max_range" options to verify an integer:

The code is as follows:


$ Var = 300;

$ Int_options = array (
"Options" => array
(
"Min_range" => 0,
& Quot; max_range & quot; = & quot; 256 & quot;
)
);

If (! Filter_var ($ var, FILTER_VALIDATE_INT, $ int_options ))
{
Echo ("Integer is not valid ");
}
Else
{
Echo ("Integer is valid ");
}
?>


Just like the code above, the options must be placed in a related array named "options. If a flag is used, it does not need to be in the array.

Because 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, visit the PHP Filter reference manual provided by W3School. You can see the available options and logos for each filter.
Verification input
Let's try to verify the input from the form.

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

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

In the following example, the input variable "email" is uploaded to the PHP page:

The code is as follows:


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:
In the above example, an input variable (email) transmitted through the "GET" method is as follows ):

Check whether "GET" type "email" input variables exist
If a variable exists, check whether it is a valid email address.
Purify input
Let's try to clear the URL from the form.

First, check whether the input data we are searching for exists.

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

In the following example, the input variable "url" is uploaded to the PHP page:

The code is as follows:


If (! Filter_has_var (INPUT_POST, "url "))
{
Echo ("Input type does not exist ");
}
Else
{
$ Url = filter_input (INPUT_POST,
"Url", FILTER_SANITIZE_URL );
}
?>


Example:
In the above example, there is an input variable (url) transmitted through the "POST" method ):

Check whether there is a "POST" type "url" input variable
If this input variable exists, purify it (delete invalid characters) and store it in the $ url variable
If the input variable is similar to the following: "http: // www. W3 # $ % S ^ % $ # ool.com.cn/", the $ url variable after cleaning should be like this:

Http://www.w3school.com.cn/filter multiple inputs
A form is usually composed of multiple input fields. To avoid repeated calls to filter_var or filter_input, 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 variable received is a name, an age, and an email address:

The code is as follows:


$ Filters = array
(
"Name" => array
(
"Filter" => FILTER_SANITIZE_STRING
),
"Age" => array
(
"Filter" => FILTER_VALIDATE_INT,
"Options" => array
(
"Min_range" => 1,
& Quot; max_range & quot; = & quot; 120 & quot;
)
),
"Email" => FILTER_VALIDATE_EMAIL,
);

$ Result = filter_input_array (INPUT_GET, $ filters );

If (! $ Result ["age"])
{
Echo ("Age must be a number between 1 and 120.
");
}
Elseif (! $ Result ["email"])
{
Echo ("E-Mail is not valid.
");
}
Else
{
Echo ("User input is valid ");
}
?>


Example:
In the above example, there are three input variables (name, age and email) transmitted through the "GET" method)

Sets an array containing the name of the input variable and the filter used for the specified input variable.
Call the filter_input_array function. parameters include GET input variables and the array you just set.
Checks whether the "age" and "email" variables in the $ result variable contain invalid input. (If illegal input exists ,)
The second parameter of the filter_input_array () function can be the ID of an array or a single filter.

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

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

It must be an associated array, and the input variable contained in it is the key of the array (for example, "age" input variable)
The value of this array must be the ID of the filter, or an array that specifies the filter, flag, and options.
Use 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 data filtering.

You can create your own custom functions or use existing PHP functions.

You are required to use the filter function, which is the same as the method of the specified option.

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

The code is as follows:


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 as follows:

Peter is a great guy! Example:
In the preceding example, convert all "_" into spaces:

Create a function that replaces "_" with spaces.
Call the filter_var () function. its parameter is the FILTER_CALLBACK filter and the array containing our function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.