Php Filters
Php Filters :
1.
A php filter is used to validate and
filter data coming from insecure sources in the server side.
2.
The php filter extension is designed to
make data filtering easier an quicker.
3.
Almost all web applications depend on
external input.
The
external data is coming from :-
a.
The data submited by the client using a
form.
b.
From cookies submitted by client.
c.
From session variables.
d.
From database.
e.
From a web service etc.
By
using php filters you can be sure that your application gets the correct input
type.
To
filter a variable , use one of the following filter functions.
1. Filter_var() : filters
a single variable with a specified filter.
2. Filter_var_array():
filter several variables with the same or different filters.
3. Filters_input():
get one input variable and filter it.
4. Filter_input_array():
get several input variables and filter them with the same or different filters.
The predefined validating filter
constants of php are :
1. FILTER_VALIDATE_INT (257)
2. FILTER_VALIDATE_BOOLEAN (258)
3. FILTER_VALIDATE_FLOAT
4. FILTER_VALIDATE_EMAIL (274)
5. FILTER_VALIDATE_URL
6. FILTER_VALIDATE_IP
7. FILTER_VALIDATE_REGEXP
Filter Example program :
<?php
$age = "25A";
$res = filter_var($age,FILTER_VALIDATE_INT);
if($res)
echo "age is valid";
else
echo "age is invalid";
echo "<br><br>";
$email ="chandu@gmail.com";
$res =
filter_var($email,FILTER_VALIDATE_EMAIL);
if($res)
echo "email is valid";
else
echo "email is invalid";
?>
OutPut:
age is invalid
email is valid
Filter_Test.html:
<html>
<body bgcolor="cyan">
<form
action="filterTest1.php" method="get">
<h1> age : <input
type="text" name="t1" size="5">
<br><br>
Email : <input
type="text" name="t2" size="30">
<br><br>
website url : <input
type="text" name="t3" size="35">
<br><br>
<input
type="submit" >
</form>
</body>
</html>
Filter_Test1.php
<?php
echo
"<h1>";
$age_options =
array("options"=>array("min_range"=>10,"max_range"=>90));
if(!filter_input(INPUT_GET,
"t1",FILTER_VALIDATE_INT,$age_options))
echo "age is invalid<br>";
if(!filter_input(INPUT_GET,"t2",FILTER_VALIDATE_EMAIL))
echo "email is
invalid<br>";
if(!filter_input(INPUT_GET,"t3",FILTER_VALIDATE_URL))
echo "web site address is
invalid";
?>
To chaeck whether a parameter with name of “email” submitted by
the client using post method or not.
<?php
If(!filter_has_var(INPUT_POST,”email”))
{
Echo(“input type
doesnot exist”);
}
?>
Filter To
validate multiple data:
<?php
$filter =
array("age"=>array("filter"=>FILTER_VALIDATE_INT,"options"=>array("min-range"=>10,"max-range"=>90)),"email"=>FILTER_VALIDATE_EMAIL);
$result =
filter_input_array(INPUT_GET,$filter);
if(!$result["age"])
{
echo ("age must be number between 10
and 90.<br>");
}
elseif
(!$result["email"]) {
echo ("email is
not valid.<br>");
}
else {
echo ("user input
is valid");
}
?>
Hey you have shared valuable content. Very brief and descriptive as well about the PHP Filters.Thanks for sharing such a nice information with us on PHP and related topics. Very useful lines and to the point. I appreciate your effort , keep sharing such wonderfull things.
ReplyDeleteOOPS Interview Questions Answers
PHP-MySQL Interview Questions Answers
80 PHP functions asked in Interview