Search Phppot Demo About Contact Me
PHP PHP Input Filtering Hi, I'm Vincy. I help build
Learn PHP websites, grow businesses, big
PHP Introduction by Vincy. Last modified on July 2nd, 2022. and small. If that is what you
PHP Basics want, contact me.
PHP Shopping Cart PHP is one of the easy-handling languages which makes developers
I'm currently available for freelance work.
Payment Gateway comfortable to work with. It provides functions to sanitize and validate the
Contact Form outside user input. These functions are in the PHP filters extension.
[email protected] PHP Login Registration
This extension is enabled by default as of the PHP version 5.2.0. It can be
Comments System
Types, Variables & Operators explicitly configured with the PHP configuration file. The outsiders’ can send
PHP Strings their input in many ways. For example, they can post the input via an HTML Get Connected
PHP Arrays form, send API params via REST clients and more.
PHP Functions
These inputs have to be sanitized and validated before processing them. We
PHP OOPS
have seen the example of doing client-side validation.
Event Management System
PHP Mail
PHP filters extension provides functions and filter constants to validate the
PHP Forms
different types of input. The filter_list() function will return the array of filters
Advanced Popular Tutorials
supported by this extension.
PHP AJAX
Simple PHP Shopping Cart
RESTful API These filters will remove the unexpected data from the user input and validate
PHP Databases Stripe Payment Gateway Integration using
the format after sanitization. In this post, we are going to see the list of filter PHP
PHP Responsive Image Gallery functions and their use. Also, I have added an example code for validating the
User Registration in PHP with Login: Form
using CSS Media Queries username and email using the PHP filter functions. The name and email data with MySQL and Code Download
PHP MyAdmin will be posted via a HTML form.
PHP Contact Form
Prevent SQL Injection in PHP
How to Create Dynamic Stacked Bar,
How to Import SQL into a
Database using PHP PHP Filter Functions Doughnut and Pie charts in PHP with Chart.js
How to Backup MySQL
Database using PHP These are the list of PHP filter functions available in the PHP filter extension.
Restore MySQL Database filter_has_var()
using PHP Testimonials
Import Excel File into MySQL This function is used to check if the specified type of the variable name exists
Database using PHP “From initial enquiry to wrap up, Vincy
or not.
produced technically astute assets which
PHP Database Backup Client
for MySQL enabled our team back in The Netherlands to
filter_has_var(int $input_type, string $var_name): bool deliver a rock-solid product ...”
MySQL Fetch using PHP
Access MySQL from PHP Sal Souza, Ofamfa, The Netherlands
The possible values of $type are INPUT_GET, INPUT_POST, INPUT_COOKIE,
PHP CRUD with MySQL
INPUT_SERVER, or INPUT_ENV. The $variable_name is the index variable to be
CRUD with MySQLi Prepared View More Testimonials
used to check the input.
Statement using PHP
PHP PDO CRUD filter_id()
Object-Oriented CRUD System
using PHP PDO and MySQL This function will return the id of the specified filter_nam.
Amazon like Product Category
Tree filter_id(string $filter_name);
PHP Input Filtering
Filtering MySQL Results by filter_input_array()
Applying PHP Conditions
Multi-Select Dropdown Filter in It accepts an array of mixed filter definitions to validate the input type.
PHP with Database Search
Sorting MySQL Column using filter_input_array ( int $type, mixed $definition, bool $add_empty
PHP
DataTables Server-side
Processing using PHP with The filter definition and add_empty arguments are optional. The $add_empty
MySQL will be TRUE by default to return NULL for the unset variable index.
Column Search in DataTables
using Server-side Processing filter_input()
Loading List box Dynamically
The PHP filter_input function validates input type with a single filter id instead
Update/Delete Multiple Rows
using PHP of the mixed definition we have used for filter_input_array.
PHP Pagination
filter_input ( int $type, string $variable_name, int $filter = FIL
Ajax Pagination with Tabular
Records using PHP and jQuery
MySQL BLOB using PHP
filter_list()
PHP Search and Pagination
using PDO
As we have mentioned above in the PHP filter introduction, this function
Bootstrap Pagination Example returns all the list of filters supported by this extension.
in PHP
PHP Sessions and Cookies
<?php
Error and Exception Handling filter_list();
File Upload ?>
File Import Export
Files and Directories filter_var_array()
PHP Date Time
PHP XML This function accepts an array of input data and filters definition with the
PHP CSV add_empty flag to validate the input array.
PHP JSON
PHP Code Samples filter_var_array ( array $data , mixed $definition , bool $add_emp
Library
PHP Freelancer
filter_var()
jQuery
JavaScript It takes a single input and filter id for validation.
WordPress filter_var ( $variable , int $filter = FILTER_DEFAULT , mixed $opt
MySQL
CSS
PHP Example: Validating HTML Form Posts using
Filters Functions
Share this page
In this example, I have created a HTML form to let the user enter their name
Share
and email address. On submitting this form the input data are posted to the
PHP file. In PHP code, the $_POST data are sanitized and validated using the
Tweet
PHP filter function filter_var.
Share HTML Code to Display the Form and Client-Side Validation Script
The below code shows the HTML for displaying the form with the user name
and email address field. On submitting this form the fnSubscribe() JavaScript
function is called and to do the not-empty check for the form fields in the
client-side.
<form name="frmSubscription" action="" method="POST"
onSubmit="return fnSubscribe();">
<input type="text" name="userName" /><br /> <input type="text"
name="userEmail" /><br /> <input type="submit" name="subsc
value="Subscribe" />
</form>
function fnSubscribe() {
if (document.frmSubscription.userName.value == "") {
return false;
}
if (document.frmSubscription.userEmail.value == "") {
return false;
}
return true;
}
PHP Filter Sanitization and Validation Code
The following PHP code applies sanitization and validation filter on the form
post data. After filtering the user input posted via the HTML form, I have
created the INSERT query to add the user data to the database.
I used FILTER_SANITIZE_STRING and FILTER_SANITIZE_EMAIL filter to sanitize
the username and email data respectively. And then, I used
FILTER_VALIDATE_EMAIL to check if the email data is in a valid format.
<?php
if (! empty($_POST["subscribe"])) {
$userName = filter_input(INPUT_POST, "userName", FILTER_SANITI
$userEmail = filter_input(INPUT_POST, "userEmail", FILTER_SANI
$userEmail = filter_var($userEmail, FILTER_VALIDATE_EMAIL);
if (! empty($userName) && ! empty($userEmail)) {
$conn = mysqli_connect("localhost", "root", "test", "blog_
$query = "INSERT INTO tbl_users (userName, userEmail) VALU
mysqli_query($conn, $query);
mysqli_close($conn);
} else {
$message = "All fields are required";
}
}
?>
download
Written by Vincy, a web developer with 15+ years of
experience and a Masters degree in Computer Science. She
specializes in building modern, lightweight websites using
PHP, JavaScript, React, and related technologies. Phppot
helps you in mastering web development through over a
decade of publishing quality tutorials.
Comments to “PHP Input Filtering”
Gideon Birimuye
December 22, 2013 at 2:38 pm
Thanks a bunch vincy.
Reply
Leave a Reply
Comment
Name *
Email *
Post Comment
Related Tutorials
★ Import Excel File into MySQL Database using PHP
★ MySQL BLOB using PHP
★ Loading List box Dynamically
↑ Back to Top
Looking for an expert PHP freelance web developer?
Do you want to build a modern, lightweight, responsive website quickly? Contact Me
Free Weekly Newsletter
Contact | LinkedIn | Facebook | © 2013 - 2025 Phppot
Enter your email here Subscribe
Privacy Policy Terms of Use Cancellation & Refund Policy
Privacy guaranteed, no spam ever.