Filter_var (variable,filter,options): variable required, filter optional, options specify an array containing the flags/options. Check the possible flags and options for each filter.
Input_type:input_get,input_post,input_cookie,input_env,input_server,input_session (not yet inplemented), INPUT_ REQUEST (not yet inplemented)
Filter: Optional, specifies the ID of the filter to be used. The default is filter_sanitize_string.
This function does not need to call Filter_input () repeatedly, which is useful for filtering multiple input variables.
|
Filter_validate_int |
Validates a value with a certificate within a specified range |
|
Filter_validate_boolean |
Returns True if "1", "true", "on", "Yes"
Returns False if "0", "false", "off", "no"
Otherwise, NULL is returned |
| Filter_validate_float |
Verifying values with floating-point numbers |
| Filter_validate_url |
Verify the value as a URL |
| Filter_validate_email |
Verify the value as an e-mail |
| Filter_validate_ip |
Verify the value as an IP address |
Filter_sanitize_url: The filter removes all illegal URL characters from the string.
There are two types of filters.
Validating filter:
Used to validate user input
Strict formatting rules
Returns the expected type if successful, or false if it fails
Sanitizing Filter:
Used to allow or disallow characters specified in a string
Unlimited data formatting rules
Always return character creation
Verify Input:
The first thing we need to do is confirm the existence of the input data we're looking for.
We then use the Filter_input () function to filter the input data.
if (!filter_has_var (input_get, "email")) {//check for the existence of a variable of the specified input type
Echo ("Input type does not exist");
}else{
if (! Filter_input (input_get, "email", filter_validate_email)) {//Get input from outside the script and filter
echo "e-mail is not valid";
}
}
Purifying Input:
First confirm that there is the input data we are looking for.
Then, use the Filter_input () function to purify the input data.
if (!filter_has_var (input_post, "url")) {//detects if there is an input variable URL for POST method transfer
Echo ("Input type does not exist");
}else{
$url =filter_input (input_post, "url", filter_sanitize_url);//If present, purify (remove illegal characters) and present it in the
}
If the input variable is similar to this: "Http://www.W3 non-O-ol.com.c character n/", then the purified $url variable should be this:
http://www.W3School.com.cn/
Filter Multiple Inputs:
A form is typically composed of multiple input fields. To avoid repeating calls to Filter_var or filter_input, we can use the 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 e-mail address:
$filters = array
(
"Name" = = Array
(
"Filter" =>filter_sanitize_string
),
"Age" = = array
(
"Filter" =>filter_validate_int,
"Options" =>array
(
"Min_range" =>1,
"Max_range" =>120
)
),
"e-mail" = Filter_validate_email,
);
$result = Filter_input_array (Input_get, $filters);
if (! $result ["Age"])
{
Echo ("Age must is a number between 1 and 120.
");
}
ElseIf (! $result ["email"])
{
Echo ("e-mail is not valid.
");
}
Else
{
Echo ("User input is valid");
}
?>
Explain:
1. Set an array that contains the name of the input variable and the filter for the specified input variable
2. Call the Filter_input_array function, which includes the get input variable and the array you just set
3. Detect if age and email in the $result variable have illegal input than the wolf.
using the filter Callback:
You can call a custom function and use it as a filter. In this way, we have full control over the data filtering.
function Convertspace ($string) {
Return Str_replace ("_", "", $string);
}
$string = "peter_is_a_greate_boy!";
Echo Filter_var ($string, Filter_callback,array ("Options" = "convertspace"));
Explain:
1. Create a function to replace "_" with a space
2. Call the Filter_var function, whose arguments are the Filter_callback filter and the array that contains our functions