PHP filter_input() function

Pagsasaayos at Paggamit

Ang function na filter_input() ay kumakakuha ng input mula sa labas ng script at naglilipat ng filter.

Ang function na ito ay ginagamit upang patunayan ang mga variable na galing sa hindi ligtas na pinagmulan, tulad ng input ng user.

Ang function na ito ay maaaring kumuha ng input mula sa iba't ibang pinagmulan:

  • 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 (e.g., FILTER_VALIDATE_EMAIL) or an ID number (e.g., 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