Home arrow PHP arrow Page 3 - Cleaning up Array Elements, POST and GET Requests with Filters in PHP 5

Sanitizing strings from GET and Post requests, cookies and sessions - PHP

Welcome to the final episode of a series that shows you how to use filters in PHP 5. Made up of nine parts, these articles show you how to utilize the numerous checking filters that come with the filter library. You can use them to thoroughly validate the incoming data handled by your PHP programs, without having to spend a long time coding custom functions or class methods.

TABLE OF CONTENTS:
  1. Cleaning up Array Elements, POST and GET Requests with Filters in PHP 5
  2. Review: the FILTER_SANITIZE_STRING filter
  3. Sanitizing strings from GET and Post requests, cookies and sessions
  4. Using callback functions and building arrays of filters
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
September 09, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I expressed in the section that you just read, the filter library also has the ability to directly sanitize data that comes from GET and POST requests and from cookies and sessions as well. These options can be pretty useful alternatives to the validation filters covered in previous articles of the series. They can be implemented with a brand new function called “filter_input().”

To demonstrate how this function can be utilized, below I created some basic examples that show how to filter variables coming from GET and POST requests, and from sessions and cookies as well. Here they are:

// example on using the INPUT_GET filter

echo filter_input(INPUT_GET, "age", FILTER_SANITIZE_NUMBER_INT);

 

// example on using the INPUT_POST filter

echo filter_input(INPUT_POST, "age", FILTER_SANITIZE_NUMBER_INT);

 

// example on using the INPUT_REQUEST filter

echo filter_input(INPUT_REQUEST, "age", FILTER_SANITIZE_NUMBER_INT);

 

// example on using the INPUT_COOKIE filter

echo filter_input(INPUT_COOKIE, "age", FILTER_SANITIZE_NUMBER_INT);

 

// example on using the INPUT_SESSION filter

echo filter_input(INPUT_SESSION, "age", FILTER_SANITIZE_NUMBER_INT);

Definitely, you should be able to quickly grasp how the “filter_input()” function does its business, since its usage is extremely intuitive. In the first three examples shown above, the function has been used for filtering a fictional “age” variable inputted via a GET and POST web form (or an eventual hyperlink, too), while the remaining code samples illustrate how to sanitize data stored in a session variable and a cookie. Simple to code and read, isn’t it?

Well, at this stage I’m sure that you’re familiar with using the “filter_input()” function for validating GET, POST and COOKIE data in a truly painless fashion. Therefore, the last topics that I’m going to discuss in this article will be focused on showing how to utilize the filter library with callback functions, and how to create an array of filters.

As usual, to see how these tasks will be accomplished, please read the following section. We’re almost finished.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: