Laravel
308
w3alert.com
20-02-2020
The PHP Filter Function is used to filter the external input data.
The filter_var() function is a built-in function in PHP which is used to validate and sanitize the input data. PHP filter_var() function filters a single variable with a applied filter.
Syntax:
filter_var( variable, filter, options );
There are three parameters in PHP filter_var() function. They are shown below:
Variable: Value to be filtered.
Filter: This indicates which filter to apply.
Options: It specifies one or more flags/options to use.
Example: The following example to understanding the PHP filter_var() function.
Example 1. Create a program to validate the data with the PHP filter_var() function.
<?php $email = "[email protected]"; // Given Email Address. $validate = filter_var($email, FILTER_VALIDATE_EMAIL); // For Validating given Email-address. if (!$validate == FALSE) { echo "Valid Email" . "<br>" . $validate; } else { echo "Invalid Email"; } ?>
Example 2. Create a program to sanitize the data with the PHP filter_var() function.
<?php $email = "abc///@gmail.com"; // Given Email Address. $sanitize = filter_var($email, FILTER_SANITIZE_EMAIL); // For Sanitizing given Email-address. echo "The Sanitize Email is:" . $sanitize; ?>
Example 3. Create a program to validate and sanitize the data with the PHP filter_var() function.
<?php $email = "abc///@gmail.com"; /*To Sanitize and Validate the data*/ $sanitize = filter_var($email, FILTER_SANITIZE_EMAIL); $validate = filter_var($sanitize, FILTER_VALIDATE_EMAIL); if ($validate == FALSE) { echo "Invalid Email"; } else { echo "Valid and Sanitize Email" . "<br>" . $validate; } ?>
The filter_has_var() function is used to check whether if a variable of a specified input type exists or not. When a variable has an input type, it returns TRUE otherwise it returns FALSE.
Syntax:
filter_has_var( data_type, variable );
There are two parameters in PHP filter_has_var() function.
Data_type: It is used to specify the type of input to check. INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, INPUT_ENV.
Variable: Name of a variable to check.
Example: The following example to understanding the PHP filter_has_var() function.
Create a program to check the input type exists or not with the PHP filter_has_var() function.
<?php if (isset($_REQUEST["submit"])) { if (filter_has_var(INPUT_POST, "name")) { echo "Name Found"; } else { echo "Name not found"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Docment</title> </head> <body> <form action="" method="POST"> Name: <input type="text" name="name" id="name"> <input type="submit" name="submit" value="submit"> </form> </body> </html>
The PHP filter_list () function is used to return a list of all supported filter names.
Syntax:
filter_list();
Note: PHP filter_list() function does not require any parameters to return a list of all supported filter names.
Example: The following example to understanding the PHP filter_list() function.
Create a program to return a list of all supported filter names with the PHP filter_list() function.
<?php $list = filter_list(); print_r($list); ?>
The filter_input () function is a built-in function in PHP used to derive a specific external variable by name and filter it.
The PHP filter_input () function is used to validate variables from insecure sources, such as user input from a form. This function is very useful to prevent some potential security threats such as SQL injection.
Syntax:
filter_input( data_type, varible, filter, options );
There are four parameters in PHP filter_input() function.
Data_type: It is used to specify the type of input to check. INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, INPUT_ENV.
Variable: Name of a variable to check.
Filter: This indicates which filter to apply.
Options: It specifies one or more flags/options to use.
Example: The following example to understanding the PHP filter_input() function.
Create a program to derive a specific external variable by name with the PHP filter_input() function.
<?php if (isset($_REQUEST["submit"])) { if (isset($_GET["email"])) { if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL) === false) { echo("Valid Email"); } else { echo("Invalid Email"); } } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Docment</title> </head> <body> <form action="" method="GET"> Name: <input type="email" name="email"> <input type="submit" name="submit" value="submit"> </form> </body> </html>
In PHP, The filter_id() function is used to get the filter ID of a specified filter name.
Syntax:
filter_id( filter_name );
Note: The PHP filter_id() function accepts a single parameter and holds the parameter filter_name.
Example: The following example to understanding the PHP filter_id() function.
Create a program to get the filter ID of a specified filter name with the PHP filter_id() function.
<?php /* use filter_id() function */ $url_id = filter_id("validate_url"); $email_id = filter_id("validate_email"); echo $url_id . "<br>"; // filter id. echo $email_id; // filter id. ?>
The filter_var_array() function is a predefined function in PHP that derive multiple values stored in the variables and optionally filter them.
Syntax:
filter_var_array( data_array, arrguments, add_empty );
There are three parameters in PHP filter_var_array() function.
Data_array: This is a necessary parameter that specifies an array containing data to filter.
Arguments: This is an optional parameter that specifies an array of filter arguments. A valid array key is a variable name and a valid value filter ID, or an array specifying filters, flags, and options. This parameter can also be a single filter ID, if so, all values of the input array are filtered by the specified filter. A filter ID can be an ID name or an ID number.
Add_empty: This is also an optional parameter. Returns a boolean value. TRUE add missing keys as NULL to the return value. The default value is TRUE.
Example: The following example to understanding the PHP filter_var_array() function.
Create a program to derive multiple values stored in the variables with the PHP filter_var_array() function.
<?php $arr = array( 'username' => 'John Lee', 'country' => 'Canada', 'email' => '[email protected]' ); $arr_data = filter_var_array($arr); print_r($arr_data); ?>
The filter_input_array() function is a built-in function in PHP that is used to derive external variables and filter them when specified. This function is similar to the filter_input() function but the only difference is filter_input() filters the single value but filters the entire array according to the options given in filter_input_array().
Syntax:
filter_input_array( data_type, definition, add_empty );
There are three parameters in PHP filter_input_array() function.
Data_array: This is a necessary parameter that specifies an array containing data to filter.
Definition: This is an optional parameter. This filter specifies an array of arguments. A valid array key as the variable name and a valid value as the filter name or ID, or an array specifying filters, flags, and options. This parameter can also be a single filter name/id like filter_input(), then all values in the input array are filtered by the specified filter.
Add_empty: This is also an optional parameter. Returns a boolean value. TRUE add missing keys as NULL to the return value. The default value is TRUE.
Example: The following example to understanding the PHP filter_input_array() function.
Create a program to derive external variables and filter them when specified with the PHP filter_input_array() function.
<?php if (isset($_REQUEST["submit"])) { if (isset($_GET["email"])) { $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" => 100 ) ), "email"=> FILTER_VALIDATE_EMAIL, ); print_r(filter_input_array(INPUT_GET, $filters)); echo("Valid Email"); } else { echo("Invalid Email"); } } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Docment</title> </head> <body> <form action="" method="GET"> Name: <input type="text" name="name" style="padding: 2.5px;margin: 10px;"> <br> Age: <input type="text" name="age" style="padding: 2.5px;margin: 10px;margin-left: 21px;"> <br> Email: <input type="email" name="email" style="padding: 2.5px; margin: 10px;"> <br> <input type="submit" name="submit" style="padding: 2.5px; margin: 10px;"> <br> </form> </body> </html>