php filter var
In this lesson we will talk about how to filter variables in PHP
But why we need to filter variables, well let me give you an example:
when someone want to enter his gmail account or facebook account he will enter his email and his password but what if he want to put a virus in this field instead of his username or password so we must filter this inputs to make sure that is not some codes or other things.
To filter a variable we use this function filter_var($var,filter) and as you see it have two parameters
The first is the variable that we want to filter it and the second one is the type of filtering
There is three types of filtering
FILTER_VALIDATE_EMAIL
FILTER_VALIDATE_URL
FILTER_VALIDATE_INT
So let's do an example about php filter var
<?php
$number="123456" ;
if(filter_var($number,FILTER_VALIDATE_INT)){
echo "this is a number";
}else{
echo "this is not a number";
}
?>
The result is
$number="123456a" ;
if(filter_var($number,FILTER_VALIDATE_INT)){
echo "this is a number";
}else{
echo "this is not a number";
}
?>
The result is
FILTER_SANITIZE_EMAIL
FILTER_SANITIZE_URL
FILTER_SANITIZE_NUMBER_INT
But what is the difference between them, the FILTER_SANITIZE_NUMBER_INT will delete all what is not a number
let's do an example
<?php
$number="123456abc" ;
$num=filter_var($number,FILTER_SANITIZE_NUMBER_INT);
echo $num;
?>
You will see this on the sccreen
Read More
In this lesson we will talk about how to filter variables in PHP
But why we need to filter variables, well let me give you an example:
when someone want to enter his gmail account or facebook account he will enter his email and his password but what if he want to put a virus in this field instead of his username or password so we must filter this inputs to make sure that is not some codes or other things.
To filter a variable we use this function filter_var($var,filter) and as you see it have two parameters
The first is the variable that we want to filter it and the second one is the type of filtering
There is three types of filtering
FILTER_VALIDATE_EMAIL
FILTER_VALIDATE_URL
FILTER_VALIDATE_INT
So let's do an example about php filter var
<?php
$number="123456" ;
if(filter_var($number,FILTER_VALIDATE_INT)){
echo "this is a number";
}else{
echo "this is not a number";
}
?>
The result is
Because the variable is a number, if you don't undestand what is if go to the php if else lesson
Let's try with this php filter var code
<?php$number="123456a" ;
if(filter_var($number,FILTER_VALIDATE_INT)){
echo "this is a number";
}else{
echo "this is not a number";
}
?>
The result is
There is other three type of filtering
FILTER_SANITIZE_URL
FILTER_SANITIZE_NUMBER_INT
But what is the difference between them, the FILTER_SANITIZE_NUMBER_INT will delete all what is not a number
let's do an example
<?php
$number="123456abc" ;
$num=filter_var($number,FILTER_SANITIZE_NUMBER_INT);
echo $num;
?>
You will see this on the sccreen
Always use filtering in your site or other insecure input
You must get used to this filtering type to write it without referring to the source
This was the php filter var, any question just type it in a comment
See you next time