D 2014-03-29T04:19:13.314 L input N text/x-markdown P f4c04016bc16865cc9cb43a73cdc89630146bc8d U mario W 17195
$_REQUEST->text["content"]
Filtering functions can also be chained, as in `$_GET->text->html["title"]`. Most sanitizing methods excise unwanted literals, several validate or drop whole values, some perform escaping, and a few are just blacklists.
* This approach addresses input constraint validation at the earliest feasible entry point.
* Unifies access through a central verification mechanism, to shadow/audit unverified retrieval.
* Often permits reliable and instant target context interpolation.
Its overall API simplicity is meant to *encourage* exertion at the right place; through minimal effort.
| Method | Type | Sample | Usage |
|---|---|---|---|
| int | cast | 123 | Only numeric characters, cast to integer. |
| name | white | abc12_x3 | Alphanumeric symbols only. |
| id | white | xy_2.1 | Alphanumeric chars, dot and underscore. |
| words | white | abc def | Text with minimal interpunction (only spaces allowed). |
| text | white | Hello, World! | Common natural text with basic interpunction (including quotes, but no < >). |
| filename | filter | basename.txt | Replace all non-alphanumeric characters with underscores. |
| float | cast | 3.14159 | Cast to float. |
| boolean | cast | true, false | Converts "false/true" or "0/1" or "off/on" and "no/yes" to boolean. |
| ascii | white | Aa#*:“,\n\0~ | Characters in the ASCII range 0 .. 127 |
| nocontrol | white | Aa#*:“,\n~ | Fiilters out control characters (< 32), except \r \n \t. |
| spaces | filter | Single line | Turns linebreaks / whitespace (\r \n \t) into spaces only. |
| q | black | \“value\“ | Shorthand for addslashes. |
| escape | black | \ []“{}'$`!´&?/><|*~;^ | Broader escaping of well-known meta charactes (quotes and regex). |
| html | filter | & | htmlspecialchars (shorthand) |
| Structural | Following filters constrain specific input formats. | ||
| datetime | white | 1999-12-31T23:59:59Z | HTML5 datetime values |
| date | white | 2015-07-17 | Just date string. |
| time | white | 23:59:20.17 | Time specifier. |
| color | white | #FF5022 | Hex color value. |
| tel | white | "+1-347-2214144 | International-format telephone number. |
| iconv | filter | Convert input to UTF-8 | |
| utf7 | black | Filter some UTF-7 out. | |
| ip | white | ::1 | IPv4 or IPv6 address |
| ipv4 | white | 134.22.7.207 | IPv4 address only |
| public | white | 8.8.4.4 | Validate IP to be public. |
| white | you @gmail.com | Syntactically valid email address. | |
| url | white | Ensure URL syntax xxx:/// | |
| http | white | http:// localhost/ | More conservative http:// URL constraint. |
| uri | white | More generic URI syntax. | |
| xml | cast | Create a SimpleXML object from input. | |
| json | cast | {„key“:“value“} | json_decode() |
| purify | filter | <b>basic</b> | Utilizes HTMLPurifier |
| NOP | Virtual / control filters. | ||
| log | control | Raw value access with logging. | |
| raw | control | Raw access with E_NOTICE (is the default). | |
| disallow | control | Disallow unfiltered variable access (configurable per INPUT_DIRECT). | |
| is | control | Is a meta filter, that applies the following filter chain, then checks if the content would have passed unaffected. Returns a boolean if all constraints were matched. | |
| Parameterized | These filters require method access $_GET->default(„id“, „index“) instead of the plain array key syntax. Alternatively ellipse … syntax. |
||
| length(ID, 20) | filter | Hello Wo | Cuts strings to maximum given length. |
| range(ID, 1, 15) | white | 17 | Constrains numeric input to the given range. |
| default | filter | … | Uses default value, if no input present. |
| regex | white/black | … | Custom regular expression method ->regex("field", "/(abc)/") |
| in_array | white | a,b,c | Can be used with array parameter, or a simpler comma-separated of allowed values. |
| Multi-Apply | Following filters work on a set of input variables, instead of a single one. | ||
| array | control | Is automatically applied to input subarrays, so filters are run on each entry. | |
| list | control | Combine multiple input variables per name (comma-separated list) and apply filtering collectively; finally return a named result array. | |
| multi | control | Also grabs a list of input variables. But does not run filters on scalars within, but pass the combined set to filter functions. This is used in combination with e.g. http_build_query |
|
| Global functions | |||
| strtolower | filter | Any global function can be chained actually. It just needs to accept one parameter, modify its input (string), and return something in return. Custom userland functions can thus be utilized. | |
| urlencode | filter | ||
| strip_tags | filter | ||
| Inadvised filters | Care should be taken here. Liberal application will lead to a false sense of security. | ||
| sql | filter | Configurable PDO::quote shorthand. |
|
| mysql | filter | Shorthand to mysql_real_escape_string (doubly discouraged). |
|
| xss | black | Minimal XSS blacklist | |
->array. Which will apply successive filters on each value entry, so `$_REQUEST->text["answers"][0]` will still resolve.
But there is also ->list for *regrouping* multiple input variable names into an associative array. It's useful to apply one set of filters onto each value, but retain them as named set afterwards.
To filter and then localize three input variables, `extract` suddenly becomes a useful idiom:
extract( $_GET->list->name["user,id,tag"] );
Input names can either be passed as comma separated list, or as actual array of names. PHP 5.4 syntax allows a neat utilization of name constants `$_GET->list->text[[URLPARAM_TITLE, URLPARAM_NAME]]` then.
The ->multi wrapper instead does not traverse each subvalue. It pipes the whole named array to its downstream filter function. Its primary purpose is:
$_GET->multi->http_build_query["id,name,title"]
Which is the most concise way in the known universe to rebuild an URL-encoded string from three input variables. (No extra code was written for that in `input.php`. It just acrued as by-product.)
… symbol (with AltGr+. on Linux, ⌥+. for Apple, or Alt+0133 on Windows).
$_GET->int->range…1…59->html["minutes"]
Which still allows chaining other filters thereafter. And this syntax novelty keeps the code a bit more readable.