PHP Filters


PHP Filters:

Filters is used to validate and filter data coming from insecure sources, like user input.


Runtime Configurations

The behavior of these functions is affected by settings in php.ini:

filter.default:

Filter all $_GET, $_POST, $_COOKIE, $_REQUEST and $_SERVER data by this filter. Accepts the name of the filter you like to use by default.  Default is "unsafe_raw".

filter.default_flags:

Default flags to apply when the default filter is set. This is set to FILTER_FLAG_NO_ENCODE_QUOTES by default for backwards compatibility reasons. Default is "NULL".
 

PHP Filter Functions

filter_has_var()  -  Checks whether a variable of a specified input type exist
filter_id()  -  Returns the filter ID of a specified filter name
filter_input()  -  Gets an external variable (e.g. from form input) and optionally filters it
filter_input_array()  -  Gets external variables (e.g. from form input) and optionally filters them
filter_list()  -  Returns a list of all supported filter names
filter_var()  -  Filters a variable with a specified filter
filter_var_array()  -  Gets multiple variables and filter them


filter_has_var():

The filter_has_var() function checks whether a variable of a specified input type exist. This function checks the content received by the PHP page, so the variable must be sent to the page via e.g a querystring

Syntax: filter_has_var(type, variable)

Example

Check if the input variable "email" is sent to the PHP page, through the "get" method:

<?php
    if (!filter_has_var(INPUT_GET, "email")) {
         echo("Email not found");
    } else {
        echo("Email found");
    }
?>


filter_id():

The filter_id() function returns filter ID of a specified filter name.

Syntax:  filter_id(filter_name)

Example

Return the filter ID of the VALIDATE_EMAIL filter:

<?php
    $echo(filter_id("validate_email"));
?>


filter_input(): 

The filter_input() function gets an external variable (e.g. from form input) and optionally filters it. This function is used to validate variables from insecure sources, such as user input.

Syntax:  filter_input(type, variable, filter, options)

Example
Check if the external variable "email" is sent to the PHP page, through the "get" method, and also check if it is a valid email address:

<?php
if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL)) {
    echo("Email is not valid");
} else {
    echo("Email is valid");
}
?>


filter_input_array():

The filter_input_array() function gets external variables (e.g. from form input) and optionally filters them. This function is useful for retrieving/filtering many values instead of calling filter_input() many times.

Syntax: filter_input_array(type, definition, add_empty)

Example

Use the filter_input_array() function to filter three POST variables. The received POST variables is name, age and e-mail:

<?php
    $filters = array (
                             "name" => array ("filter"=>FILTER_CALLBACK,
                             "flags"=>FILTER_FORCE_ARRAY,
                             "options"=>"ucwords"
                            ),
      "age"   => array ( "filter"=>FILTER_VALIDATE_INT,
                              "options"=>array("min_range"=>1,"max_range"=>120)
                            ),
       "email" => FILTER_VALIDATE_EMAIL
       );
     print_r(filter_input_array(INPUT_POST, $filters));
?>


The output of the code above will be:

Array
  (
      [name] => Peter
      [age] => 41
      [email] => peter@example.com
  )


filter_list():

The filter_list() function returns a list of all the supported filter names.

Syntax:  filter_list()

Example
List all supported filter names:

<?php
    print_r(filter_list());
?>


filter_var():

The filter_var() function filters a variable with the specified filter.


Syntax:  filter_var(var, filtername, options)

Example

Check if $email is a valid email address:

<?php
    $email = "john.doe@example.com";

    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
      echo("$email is a valid email address");
    } else {
       echo("$email is not a valid email address");
    }
?>


filter_var_array():

The filter_var_array() function gets multiple variables and optionally filters them. This function is useful for filtering many values without calling filter_var() many times.

Syntax:  filter_var_array(data_array, args, add_empty)

Example

Use the filter_var_array() function to get multiple variables:

<?php
$data = array(
  'fullname' => 'Peter Griffin',
  'age' => '41',
  'email' => 'peter@example.com',
);

$mydata = filter_var_array($data);
var_dump($mydata);

?>


The output of the code should be:

array(3) {
  ["fullname"]=> string(13) "Peter Griffin"
  ["age"]=> string(2) "41"
  ["email"]=> string(17) "peter@example.com"
}

Comments

Popular posts from this blog

Create Desktop Application with PHP

Insert pandas dataframe into Mongodb

Add and delete columns dynamically in an HTML table