Recipe 18.3. Ensuring Input Is Filtered


18.3.1. Problem

You need to be sure that all input is filtered before being used.

18.3.2. Solution

Initialize an empty array in which to store filtered data. After you've proven that something is valid, store it in this array:

<?php /* Initialize an array for filtered data. */ $clean = array(); /* Allow alphabetic names. */ if (ctype_alpha($_POST['name'])) {     $clean['name'] = $_POST['name']; } else {     /* Error */ } ?> 

18.3.3. Discussion

By using a strict naming convention, you can more easily keep up with what input has been filtered. Always initializing $clean to an empty array ensures that data cannot be injected into the array; you must explicitly add it.

Once you adopt a technique such as the use of $clean, it is important that you only use data from this array in your business logic.

18.3.4. See Also

Recipes Recipe 9.2 to 9.9 discuss form input validation for different types of data in detail.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net