in Php

Validation and Sanitization data with PHP filters

Introduction

Validation and sanitization of a data is an important part when working with forms. Without these, will lead to security vulnerability and even break your webpage. PHP filters can be used to validate and filter data from insecure sources like form or user input.

Getting Started

PHP filters have many functions needed to check user input. To make data validation easy and quick, PHP filters were made. If we list out filters, you can see many functions. You can use them as per your requirement.

filter_var() function

filter_var() function validates and sanitize your data. It will take the variable you want to check , and the filter type as two parameters.

The above filter check will echo out 24. Now how will we use in real life scenario :

This is a simple demonstration for using php filters for validation. What about sanitizing of data ?

In all above demonstration, you can see we’ve been using FILTER_VALIDATE_INT constant. Now all we have to do is use FILTER_SANITIZE_* .

* may contain any filter available like INT, STRING

Let’s look at a simple string sanitization example :

How do we use both validation and sanitization ?

Here is an another demonstration :

Happy coding 🙂