php goodies: filter

Filter is in php by default from 5.2.0 and is a usefull library for web developer, so it is the time to make use of it.

It was created for validation and sanitization of foreign input, that is GET, POST, COOKIE, or anything that you can't trust on. With php filter one can tests if a variable is present in the current request: 

if(filter_has_var(INPUT_POST,'login')) {
//manage login
}

Using this function a check state of a checkbox in a form could be tested as well:

 $remember_login = filter_has_var(INPUT_POST,'remember_login')?1:0;

(TRUE:FALSE or whatever you want) But filter is very usefull at validate input too:

if(!filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT)) { show_404(); }

ie, do not open unidentified page. Also filter can sanitize input with sanitization filters.

All function has an array variant that permit to filter or sanitize a set of vars with a set of rules.

Manual: http://php.net/manual/en/book.filter.php

Attachments: 
AttachmentSize
Image icon filter.jpg14.9 KB