Filter in PHP

Filter in PHP


Posted in : PHP Posted on : January 10, 2011 at 6:01 PM Comments : [ 0 ]

This section contains the detail about Filter in PHP.

Filter in PHP

PHP filters is used to validate and filter the external input. Input may be user input ,cookies, web services data, server variables, Database query results etc. Filter insure us that our application gets the correct data.

For filtering a variable, use one of the following functions :

Function for  filtering a
variable
Description of the function 
filter_var()  Filters a single variable with a specified filter
filter_var_array() Filter several variables with the same or different filters
filter_input Get one input variable and filter it
filter_input_array Get several input variables and filter them with the same or different filters.
 

Example : filter_var( )

In this example, we validate an integer using filter_var( ) method :

<?php
$myint = 102;

if(!filter_var($myint, FILTER_VALIDATE_INT))
{
echo("Provided variable is not an integer");
}
else
{
echo("Provided variable is an integer");
}
?>

Output :

Provided variable is an integer.

Filtering with options :

Options are used to widen the filtering by adding options to the filter. Given below example will give you a clear idea :

<?php
$myvar=102;

$my_options = array(
"options"=>array
(
"min_range"=>0,
"max_range"=>101
)
);

if(!filter_var($myvar, FILTER_VALIDATE_INT, $my_options))
{
echo("Provided variable is not an integer");
}
else
{
echo("Provided variable is an integer");
}
?> 

Output :

Provided variable is not an integer.

Filter's type

Filters can be categorized into following types :

Validating filters:

Validating filters are used to validate user input. It checks the correctness of the format specified for input (like URL or E-Mail validating). If validation fails, it returns false on failure and if its succeed , it return the expected type.

Example : 

<?php
if(!filter_has_var(INPUT_GET, "email"))
{
echo("Input type does not exist");
}
else
{
if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL))
{
echo "E-Mail is not valid";
}
else
{
echo "E-Mail is valid";
}
}
?>

First 'if' checks the existence of the input data. Then we filter the data using  filter_input() function and FILTER_VALIDATE_EMAIL filter. Here, We are checking whether it is valid email address or not using FILTER_VALIDATE_EMAIL filter.

Sanitizing filters:

Sanitizing filters are used to restrict or allow specified characters in a String. It doesn't check the format. It always return the string.

Example :

<?php
if(!filter_has_var(INPUT_POST, "url"))
{
echo("Input type does not exist");
}
else
{
$url = filter_input(INPUT_POST,
"url", FILTER_SANITIZE_URL);
}
?>

First 'if' checks the existence of the input data. If the input variable exists, sanitize (take away invalid characters) and store it in the $url variable. If the input variable is a string like this "http://www.dev�¥�¥manua�¸�¸ls.com/", the $url variable after the sanitizing will look like this:

http://www.devmanuals.com/

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics