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.
blog comments powered by Disqus |
|
|
|
|
|
|
|