Latest web development tutorials

PHP filter_input () function

PHP Filter Reference Complete PHP Filter Reference

Definition and Usage

filter_input () function to get input (such as form input), and filtered from the outside of the script.

This function is used for variables from the non-secure sources for authentication, such as user input.

This function can take 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)

If successful, the filtered data is returned. If it fails, it returns FALSE. If the "variable" parameter is not set, it returns NULL.

grammar

filter_input(input_type, variable, filter, options)

参数 描述
input_type 必需。规定输入类型。参见上面的列表中可能的类型。
variable 必需。规定要过滤的变量。
filter 可选。规定要使用的过滤器的 ID。默认是 FILTER_SANITIZE_STRING。参见 完整的 PHP Filter 参考手册 ,查看可能的过滤器。

过滤器 ID 可以是 ID 名称(比如 FILTER_VALIDATE_EMAIL)或 ID 号(比如 274)。

options 可选。规定一个包含标志/选项的关联数组或者一个单一的标志/选项。检查每个过滤器可能的标志和选项。


Examples

In this example, we use filter_input () function to filter a POST variable. POST variable received is legitimate 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 code is as follows:

E-Mail is valid


PHP Filter Reference Complete PHP Filter Reference