Definition and usage
The filter_input () function obtains and filters the input from outside the script.
This function is used to verify variables from unsafe sources, such as user input.
This function can obtain input from various sources:
- Input_get
- Input_post
- Input_cookie
- Input_env
- Input_server
- Input_session (not yet implemented)
- Input_request (not yet implemented)
If the operation succeeds, the filtered data is returned. If the operation fails, false is returned.VariableIf the parameter is not set, null is returned.
Syntax
Filter_input (input_type, variable, filter, options)
Parameters |
Description |
Input_type |
Required. Specifies the input type. See the possible types in the list above. |
Variable |
Specifies the variable to be filtered. |
Filter |
Optional. Specifies the ID of the filter to be used. The default value is filter_sanitize_string. See the complete PHP filter function reference manual to obtain possible filters. The filter ID can be an ID name (such as filter_validate_email) or an ID (such as 274 ). |
Options |
Specifies an array containing flag/option. Check the possible flags and options of each filter. |
Example
In this example, we use the filter_input () function to filter a post variable. The accepted post variable is a valid email address.
<? Phpif (!Filter_input (input_post, 'email ', filter_validate_email)
) {Echo "E-mail is not valid";} else {echo "E-mail is valid" ;}?>
The output is similar:
Email is valid