توابع filter_input() در PHP
تعریف و استفاده
توابع filter_input() ورودیها را از خارج از اسکریپت دریافت کرده و فیلتر میکند.
این تابع برای تأیید متغیرهایی که از منابع غیر امنی میآیند استفاده میشود، مانند ورودی کاربر.
این تابع میتواند از منابع مختلف ورودی را دریافت کند:
- INPUT_GET
- INPUT_POST
- INPUT_COOKIE
- INPUT_ENV
- INPUT_SERVER
- INPUT_SESSION (Not yet implemented)
- INPUT_REQUEST (Not yet implemented)
If successful, it returns the filtered data, if failed, it returns false, if variable If the parameters are not set, it returns NULL.
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 filter. |
filter |
Optional. Specify the ID of the filter to use. 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 (such as FILTER_VALIDATE_EMAIL) or an ID number (such as 274). |
options | Specify an array containing flags/options. Check each filter's 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