Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Integrating PHP with Databases Using Relational Tables Understanding SQL Injections

Andrew Dickens
Andrew Dickens
18,352 Points

Filter input, escape output

ok so I get the 'filter input', but what does Alena mean by 'escape output'?

3 Answers

It prevents using output you might not to be used (echo'd for example).

Let's say you would want to retrieve a name value from a GET parameter. Someone might instead set Javascript code as the name value, like: http://yourwebsitedomain.nl/?name=<script>alert('Evil stuff');</script>

If you simply echo out the value without escaping, the Javascript code gets executed. So you don't want to do this:

<?php
echo $_GET['name'];  

This code might do evil things, you wan't to prevent that of course. So instead we make sure no code is being echo'd by escaping the output, like this:

<?php
echo filter_input( INPUT_GET,  'name', FILTER_SANITIZE_STRING ); // strip code from string
Andrew Dickens
Andrew Dickens
18,352 Points

ok Jeffrey you have added a filter, (you have filtered the input) I get that, but what is the escape part?

Steven Price
Steven Price
15,120 Points

Hi im not sure of the difference between filtering input and escaping output - if you filter input from a user e.g. PHP filter_input and display it in a forum surely you don't need to also escape using htmlentities()? isnt it the same thing?