⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

PHP Filters

PHP filters are used to validate and filter data coming from insecure sources, such as user input. No additional installation is required to use these filters, as they are enabled by default. When using advanced filters, we can apply various options to customize the filters for validating and sanitizing input data. Here we will look at some of the more advanced ways to use filters.

EXAMPLE
RESULT
The result will be displayed here...

The behavior of these filters is affected by settings in the php.ini file:

name description default changeable
filter.default This filter filters all $_GET, $_POST, $_COOKIE, $_REQUEST, and $_SERVER data. It accepts the name of the filter you want to use by default. See the filter list for the list of filter names. unsafe_raw PHP_INI_PERDIR
filter.default_flags Default flags to apply when the default filter is set. It is set to FILTER_FLAG_NO_ENCODE_QUOTES by default for backwards compatibility. NULL PHP_INI_PERDIR

Overview of PHP Filtering Functions

PHP offers a set of built-in functions that allow you to check for the existence of input data and filter it directly:

Function Name Description Example (Code for simulator) Action
filter_has_var() Checks if a variable of a specific type (POST/GET) exists. echo filter_has_var(INPUT_POST, 'email') ? 'Exists' : 'Does not exist';
filter_id() Returns the internal ID number of a specified filter name. echo "Email filter ID is: " . filter_id("validate_email");
filter_input() Gets an external variable and filters it immediately. echo "URL: " . filter_input(INPUT_POST, 'web', FILTER_VALIDATE_URL);
filter_input_array() Gets multiple external variables at once and filters them. $f = ['input' => FILTER_VALIDATE_INT]; print_r(filter_input_array(INPUT_POST, $f));
filter_list() Returns an indexed array of all supported filter names. print_r(filter_list());
filter_var() Filters a specific variable with a specified filter. echo filter_var('test@email.com', FILTER_VALIDATE_EMAIL) ? 'Valid' : 'Invalid';
filter_var_array() Filters multiple variables at once using an array of filters. $p = ['a' => 10]; $f = ['a' => FILTER_VALIDATE_INT]; print_r(filter_var_array($p, $f));

How Filters Work

Advanced Filter Options

FILTER_VALIDATE_IP with various flags:

FILTER_VALIDATE_URL with flags:

Using these methods, we can control the validation and sanitization of input data very precisely, which improves the security of our application.