w3schools Logo
Monday 21 May 2012
Www.w3schools.in Developer Network
HTML tutorials
CSS tutorials
php tutorials and scripts
MySql tutorials
Learn C Programming
Operating system

how to filter user submitted data easily in php
From w3schools.in

Yesterday, I saw one of my friend was working on the the contact form and was filtering the user input data(posted variables) individually. He was using a function in PHP to filter the input and using  tedious approach while calling the filtering function for each variables with coding  each of them in single line . Today, I’m going to show you how can you filter the posted variables easily using callback function in PHP.

PHP function to filter the user supplied data.

function filter_data($val)
{
  return htmlentities($val,ENT_QUOTES);
}

This is just a example of very simple function is PHP to filter the user ssubmitted data.But ,you can add more code according to your requirement to make this function robust.

Common programmer’s approach to filter submitted data in PHP

$name=filter_data($_POST['name']);
$email=filter_data($_POST['email']);
$website=filter_data($_POST['website']);

It was the approach which I’ve used in beginning of my career and most of the beginner PHP programmer use this approach to filter the posted variables.And, the above list can be long if there are more posted data.At that time, it will be very irritating to use the same kind of line in many places.

Using array_map() function to filter the posted data in PHP

As you know, POST variables are super global array in PHP and you can use array_map() function in PHP to filter the input using the callback function. Let’s see how you can filter the the posted data easily,

$post=array_map("filter_data",$_POST);

As you can, each values of POST variables is mapped into another array using a call back function filter_data() which is defined above.

Now, you can access the filtered variables easilly with $post['name'] or $post['email'] etc.


This page was last modified on 14 Sep 2011 at 16:56:05.
Gautam Kumar
EDP Manager
http://www.connectsin.com
Related Posts
» Useful functions to tighten the security in PHP
» CodeIgniter A recommendation for PHP Programmer
» PHP Framworks Why when and which
» Solving Floating point number precision lost problem in PHP
» Handling array of HTML Form Elements in JavaScript and PHP
» How to filter user submitted data easily in PHP
» Prevent form post request from another domain in PHP
» Flaw in and or logical operator php
» Default arguments functions php
» Submit Form without Refreshing Page with Jquery
» MySQL Performance Tips
» Free ajax chat applications in PHP
» Return More Than One Value From a Function in PHP
» PHP Optimization Tips
» PHP Error Handling
» Useful PHP Classes and Libraries
» Useful Tools For PHP Developers
» PHP Frameworks
» Web based HTML Editors
» 301 redirect in PHP and .htaccess
» Hiding PHP file extension
» Rredirect Browser HTTPS SSL PHP
» Tighten php security functions
» PHP Framworks