PHP filter_input() Function
Complete PHP Filter Reference Manual
Definition and Usage
The filter_input() function gets input from outside the script (such as form input) and filters it.
This function is used to validate variables from non-secure sources, such as user input.
This function can get input from a variety of sources:
- INPUT_GET
- INPUT_POST
- INPUT_COOKIE
- INPUT_ENV
- INPUT_SERVER
- INPUT_SESSION (not yet implemented)
- INPUT_REQUEST (not yet implemented)
Returns the filtered data on success, or FALSE on failure. If the "variable" parameter is not set, returns NULL.
Syntax
filter_input(input_type, variable, filter, options)
| Parameters | Description |
|---|---|
| input_type | Required. Specifies the input type. See the list of possible types above. |
| variable | Required. Specifies the variable to filter. |
| filter | Optional. Specifies the ID of the filter to use. Default is FILTER_SANITIZE_STRING. See Complete PHP Filter Reference Manualfor possible filters.
A filter ID can be an ID name (such as FILTER_VALIDATE_EMAIL) or an ID number (such as 274). |
| options | Optional. Specifies an associative array of flags/options or a single flag/option. Check the possible flags and options for each filter. |
Example
In this example, we use the filter_input() function to filter a POST variable. The POST variable received is a valid e-mail address:
The output of the code is shown below:
E-Mail is valid
Complete PHP Filter Reference Manual