• Subscribe to the RSS feed!
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Ze Balkanic Tweetup
      • on May 31, 2009
    • Moblin, Linux for netbooks
      • on May 21, 2009
    • Back
      • on
    • Happy birthday, dear magician…
      • on May 10, 2009
    • Wordpress as CMS tutorial
      • on March 14, 2009
    • New blog - Try Open Source
      • on March 11, 2009
    • Online resources for Zend Framework
      • on March 3, 2009
    • pywst - setting up web projects quickly
      • on February 22, 2009
    • Full Circle Magazine
      • on February 8, 2009
    • Trac on Ubuntu
      • on January 27, 2009
  • Recent Comments

    • Jason Gilmore
      • on June 23rd @ 5:04 am
    • slawek
      • on June 11th @ 1:29 am
    • igor
      • on June 7th @ 9:56 pm
    • Swizec
      • on June 1st @ 11:18 pm
    • Robert
      • on June 1st @ 8:12 pm
    • Eniac
      • on June 1st @ 2:17 pm
    • -1-
      • on May 31st @ 11:04 pm
    • Robert
      • on May 31st @ 10:54 pm
    • Swizec
      • on May 31st @ 10:27 pm
    • blackshtef
      • on May 31st @ 8:14 pm
  • Find me on

    • DZone
    • Google Code
    • Google Reader
    • Last.fm
    • StumbleUpon
    • Twitter
    • Vimeo
  • Friends and Blogs

    • Andrew Taylor
    • Andy Sowards
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Jani Hartikainen
    • Jasper Tandy
    • Matthew Turland
    • Matthew Weier O’Phinney
    • Miff
    • Miloš Ćuković
    • Nebojša Radović
    • Nemanja Avramović
    • Nemanja Tobić
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec Teller
    • Vladimir Stanković
    • WeAreJustCreative
    • Željko Stevanović
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Subversion
    • Trac
    • Vim
    • Zend Framework
  • Tags

    • about
    • php
    • random
    • example
    • framework
    • zend
    • ubuntu
    • site
    • blog
    • introduction
    • wordpress
    • linux
    • apache
    • setup
    • lamp
    • svn
    • open source
    • registration
    • facebook
    • comic
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
  • Photos

    Big awesome coffee mug
    A 20+ year old scotch, in a 4.5l bottle
    A chessboard from 1939
    A 20+ year old scotch, in a 4.5l bottle

  • Archives

    • May 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008

Data filtering with PHP’s Filter extension

by Robert Basic on December 15th, 2008

Today I was catching up on feeds and one of the articles lead me to GoPHP5.org, where I spent some time lurking. In the FAQ section of that site one sentence made me curios:

The Filter extension is a new security component in PHP.

Filter extension? Maybe it’s nothing new for some of you, but it is for me. I’ve never heard of it before. So I quickly hopped over to PHP.net and the Filter chapter of the manual.

The filter extension is an extension that comes by default in PHP 5.2. It is here to help us to “validate and filter data that comes from insecure sources, such as user input”. It can validate integers, booleans, floats, regular expressions, URLs, E-Mails and IPs. It can sanitize strings, integers, floats, URLs, E-Mails…

Examples

Here are some examples about what this extension is capable of. Lets assume that we get some data from a form with POST method. The 3 input fields are name, email and age (I’m not creating a real validator, but var_dump-ing the results of the filtering, to show what filter gives what kind of output).

// $_POST['name'] = "Robert hello";
var_dump(filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING));
// Output: string(12) "Robert hello"

// $_POST['email'] = "mail@example.com";
var_dump(filter_input(INPUT_POST, 'name', FILTER_VALIDATE_EMAIL));
// Output: string(16) "mail@example.com"

// $_POST['age'] = "22";
var_dump(filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT,
                        array('options' => array('min_range' => 18,
                                                'max_range' => 28)
                        )));
// Output: int(22)

With the first filter I’m using the FILTER_SANITIZE_STRING which strips down all tags and unwanted characters from our string. The second filter validates the provided E-mail address: pass it a malformed E-mail address and it will result with a boolean false. The third filter validates the age: it must be an integer and in the range between 18 and 28 (the min and max ranges are optional, I added them just for the example).

Besides input filtering it can filter variables, too:

$string = "Some funky string with html code and 'quotes'";
var_dump(filter_var($string, FILTER_SANITIZE_STRING));
// Output: string(53) "Some funky string with html code and 'quotes'"
// NOTE: the single quotes in the output are encoded as '

var_dump(filter_var($string, FILTER_SANITIZE_MAGIC_QUOTES));
// Output: string(54) "Some funky string with html code and \'quotes\'"
// NOTE: the  html tags are NOT stripped in the output

var_dump(filter_var($string, FILTER_SANITIZE_ENCODED));
// Output: string(80) "Some%20funky%20string%20with%20%3Cb%3Ehtml%3C%2Fb%3E%20code%20and%20%27quotes%27"

Play around with it, get familiar, cause this is one nice extension that will help you make more secure web sites and web apps.

Cheers!

Share this post:
  • Digg
  • description
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Reddit
  • TwitThis
  • Google
  • E-mail this story to a friend!
Tags: data, example, filter, input, php, secure.
Categories: Development, Programming.
Subscribe to the feed.

Comments: 11

Grab the comments feed

  • Jasper

  • December 15th, 2008

It’s still beyond me why this isn’t done in an OO way. Having to remember those constants is annoying!

  • Robert

  • December 15th, 2008

No one said you need to remember them. Just fire up the manual and you’re good to go ;)

  • Jasper

  • December 15th, 2008

Yeah, but that’s annoying!

  • Data filtering with PHP’s Filter extension | PHP-Blog.com

  • December 16th, 2008

[...] post: Data filtering with PHP’s Filter extension Related ArticlesBookmarksTags Data types PHP stores whole numbers in a platform-dependent [...]

  • Ross

  • December 16th, 2008

While the url filter is a bit iffy (reportedly it’s just a parse_url check) it is a very useful extension to have. Can make lots of strip_tags etc. a thing of the past.

  • Swizec

  • December 17th, 2008

Ooooh, definitely gonna be giving this a try

  • links for 2008-12-17 | Squirrel Hacker

  • December 17th, 2008

[...] Data filtering with PHP's Filter extension ~ Robert Basic Cool… I had no idea this existed (tags: php tips opensource) This entry was posted on Wednesday, December 17th, 2008 at 7:08 am and is filed under Delicious Links . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. [...]

  • Code in Tumble - Links aufgesammelt

  • December 17th, 2008

[...] Data filtering with PHP’s Filter extension: Dieser schlafende Hund aus der Schublade (frei nach Michi Glos) wurde erweckt und präsentiert eine Interessante Funktion in PHP 5.2: Filtern. [...]

  • MaddSkillz

  • January 20th, 2009

Hmm this looks like something that will be worth a shot! thanks for the post

  • Jamie Guadaldo

  • February 3rd, 2009

This is great because now idiot hackers who want to steal my website or trash my website will have a more difficult time doing so thanks to this little handy script.

Thanks to this post maybe more coders and programmers will take advantage of this nice security feature of PHP (or even better, encourage more people to use PHP to begin with).

  • Jones

  • April 3rd, 2009

This is deffently something to consider, I just get my home made PHP-based CMS hacked.. Thanks for the post.

Leave a Reply

 

Robert Basic © 2008 — 2009
Design & graphics by: Livia Radvanski
Coded by: Robert Basic
Home page last updated on January 3rd, 2009.
Frameworks used: Zend Framework, jQuery, 960 Grid System
Blog is powered by Wordpress
Subscribe: Entries — RSS & Comments — RSS