XSS filtering function in PHP

codecowboy picture codecowboy · Aug 26, 2009 · Viewed 104.3k times · Source

Does anyone know of a good function out there for filtering generic input from forms? Zend_Filter_input seems to require prior knowledge of the contents of the input and I'm concerned that using something like HTML Purifier will have a big performance impact.

What about something like : http://snipplr.com/view/1848/php--sacar-xss/

Many thanks for any input.

Answer

cletus picture cletus · Aug 26, 2009

Simple way? Use strip_tags():

$str = strip_tags($input);

You can also use filter_var() for that:

$str = filter_var($input, FILTER_SANITIZE_STRING);

The advantage of filter_var() is that you can control the behaviour by, for example, stripping or encoding low and high characters.

Here is a list of sanitizing filters.