Η συνάρτηση PHP filter_input()
Ορισμός και χρήση
Η συνάρτηση filter_input() αποκτά είσοδο από το εξωτερικό του σكрипτ και την φιλτράρει.
Η συνάρτηση αυτή χρησιμοποιείται για την επαλήθευση μεταβλητών από μη ασφαλή πηγές, όπως οι εισόδους χρηστών.
Η συνάρτηση αυτή μπορεί να αποκτήσει είσοδο από διάφορες πηγές:
- INPUT_GET
- INPUT_POST
- INPUT_COOKIE
- INPUT_ENV
- INPUT_SERVER
- INPUT_SESSION (Not yet implemented)
- INPUT_REQUEST (Not yet implemented)
If successful, the filtered data is returned, if failed, false is returned, if variable If parameters are not set, NULL is returned.
Syntax
filter_input(input_type, variable, filter, options)
Parameters | Description |
---|---|
input_type | Required. Specify the input type. See the list above for possible types. |
variable | Specify the variable to be filtered. |
filter |
Optional. Specify the ID of the filter to be used. The default is FILTER_SANITIZE_STRING. See the complete PHP Filter Function Reference Manual for possible filters. The filter ID can be an ID name (e.g., FILTER_VALIDATE_EMAIL) or an ID number (e.g., 274). |
options | Specify an array containing flags/options. Check each filter for possible flags and options. |
Example
In this example, we use the filter_input() function to filter a POST variable. The accepted POST variable is a valid e-mail address.
<?php if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)) { echo "E-Mail is not valid"; } else { echo "E-Mail is valid"; } ?>
Output similar to:
E-Mail is valid