/PHP filters validation
hashing

PHP filters validation

PHP filters validation
/**
@author : Shubham Maurya,
Email id : maurya.shubham5@gmail.com
**/

Hi all , Welcome to shubhammaurya.com , Today we are going to discuss ,
about PHP filters validation

PHP Filters

There are two terms :

Sanitizing data = Remove any illegal character from the data.
Validating data = Check if the data is in correct form or not.

PHP Filter Extension

Filters are used to sanitize and validate the external input.
Filters extension has many functions i.e needed for checking the user input, and by this data validation become quicker and easier.

PHP Functions

  1. filter_list()
  2. filter_var()
Why use filters in PHP?

To check external input. Many websites and web applications receives the external input from the user and they can be in these one of
the format –

  1. Cookies
  2. Variables
  3. Database query search results
  4. Taking input from a form
  5. Web services data
Note :-  

You have to always validate external data. Wrong submitted data can lead to many security problems and harm your webpages.
By using the filters, you can get the correct external inputs from the user.

Filters Explaination

filter_list()
– uses to get the filter name and filter id.

filter_var()
– can both sanitize and validate data
– filters a single variable with a specific chosen filter.They are like :-
– The type of check to use
– The variable you want to check

Some of the filter_var() function example

To sanitize a string
It remove all the HTML tags from any string.
Code : filter_var($your_variable, FILTER_SANITIZE_STRING);

 

<?php
$code="<p>Remove paragraph</p>";
$remove= filter_var($code, FILTER_SANITIZE_STRING);
echo $remove;
?>;

 

To validate an integer
It checks that the given variables is an interger or not.
Code : filter_var($your_int_variable, FILTER_VALIDATE_INT);

 

<?php

$num=100;
if(!filter_var($num, FILTER_VALIDATE_INT)==false){
echo "Valid";
}
?>

 

To validate an IP Address
It checks that the given variable has a valid ip or not.
Code : filter_var($ip_variable, FILTER_VALIDATE_IP);

 

<?php

$ip=100;
if(!filter_var($ip, FILTER_VALIDATE_IP)==false){
echo "Valid IP";
}
?>

 

To sanitize and validate an Email Address
It removes all the illegal characters from the email and then check that, it is a valid email or not.
Code : filter_var($email, FILTER_SANITIZE_EMAIL);

 

<?php

$email="maurya.shubham5@gmail.com";
$email=filter_var($email, FILTER_SANITIZE_EMAIL);

if(!filter_var($email, FILTER_VALIDATE_EMAIL)==false){
echo "Valid Email";
}
?>

Complete PHP validation here, you can check by go to PHP main website : Click here to Go

 

Comment Below, If any problem occurs.

STAY CONNECTED FOR MORE

Hi , My Name is Shubham Maurya and i am currently working as a Project Manager.