PHP Reference ยท Functions and Classes
PHP filter_input() function
Reference for PHP filter_input(): read and validate a value from GET, POST, cookies or server data in one call, with the null and false return values explained.
filter_input() fetches one variable from a request source (INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER or INPUT_ENV) and runs it through a filter, the same filters that filter_var() uses. It replaces the pattern "check isset($_GET['id']), then validate $_GET['id']" with one call.
It has an important quirk: it reads the original request data that PHP received, not the $_GET and $_POST arrays as you may have changed them. That makes it safe against code that overwrites the superglobals, but it also means you cannot test it by assigning to $_GET.
$id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT); if ($id === null) { /* missing */ } if ($id === false) { /* present but invalid */ }
Try it
This runs as a command-line script, so there is no real request. The first example shows what that means; the second does the same job on a simulated $_GET with filter_var().
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
filter_input( int $type, string $var_name, int $filter = FILTER_DEFAULT, array|int $options = 0 ): mixed
| Name | Description |
|---|---|
$type |
The source: INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER or INPUT_ENV. |
$var_name |
The name of the variable inside that source. |
$filter |
A filter constant, as for filter_var(). Default FILTER_DEFAULT. |
$options |
Flags, or an array with "options" and "flags". Use FILTER_REQUIRE_ARRAY for array inputs such as tags[]. |
Return value: the filtered value on success; false if the filter fails; null if the variable does not exist. FILTER_NULL_ON_FAILURE swaps false and null: a missing variable returns false and a failed filter returns null. Related functions: filter_input_array() filters many variables at once and filter_has_var() tests for existence.
Errors: an unknown $type throws a ValueError.
Usage notes
- Tell missing from invalid. With the default flags
nullmeans "not sent" andfalsemeans "sent but invalid", so always compare with===. - It ignores changes to superglobals. Assigning
$_GET["id"] = "42"does not affectfilter_input(), as the first example shows, and in a CLI script it always finds nothing. Usefilter_var()on array values in tests, or pass the source into your function. INPUT_SERVERandINPUT_ENVcan be unreliable on some SAPIs (notably FastCGI setups), so$_SERVERis often the practical choice for server variables.- Filtering is not output escaping. Even a validated string must go through
htmlspecialchars()when printed in HTML, and through prepared statements when used in SQL. - Frameworks wrap the request in an object and validate there;
filter_input()is mostly for plain scripts. - Arrays: for
name="tags[]"fields passFILTER_REQUIRE_ARRAYorFILTER_FORCE_ARRAYand filter each element. - Sessions are not an input source. There is no
INPUT_SESSIONorINPUT_REQUEST; usefilter_var()for those.
Related
filter_var(),$_GET,$_POST,$_SERVER,htmlspecialchars()- Lessons: Form validation, Forms