HomePHP 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.
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:
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.