title: Output or Input filtering?
body: <p>Output or Input filtering?</p>
  
  <p>I constantly see people writing "filter you inputs", "sanitize your inputs", don't trust user data, but I only agree with the last one, where I consider trusting any external data a bad idea even if it is internal relative to the system.</p>
  
  <p><strong>Input filtering:</strong>
  The most common that I see.
  Take the form post data or any other external source of information and define some boundaries when saving it, for example making sure text is text, numbers are numbers, that sql is valid sql, that html is valid html and that it does not contain harmful markup, and then you save the "safe" data in the database. </p>
  
  <p>But when fetching data you just use the raw data from the database.</p>
  
  <p>In my personal opinion, the data is never really safe.
  Although it sounds easy, just filter everything you get from forms and url's, in reality it is much harder than that, it might be safe for one language but not another.</p>
  
  <p><strong>Output filtering:</strong>
  When doing it this way I save the raw unaltered data, whatever it might be, with prepared statements into the database and then filter out the problematic code when accessing the data, this has it's own advantages:
  <em>This adds a layer between html and the server side script.</em>
  which I consider to be data access separation of sorts.</p>
  
  <p>Now data is filtered depending on the context, for example I can have the data from the database presented in a html document as plain-escaped-text, or as html or as anything anywhere.</p>
  
  <p>The drawbacks here are that you must not ever forget to add the filtering which is a little bit harder than with input filtering and it uses a bit more CPU when providing data.</p>
  
  <p>This does not mean that you don't need to do validation checks, you still do, it's just that you don't save the filtered data, you validate it and provide the user with a error message if the data is somehow invalid.</p>
  
  <p>So instead of going with "filter your inputs" maybe it should be "validate your inputs, filter your outputs".</p>
  
  <p>so should I go with "Input validation and filtering" or "Input validation and output filtering"?</p>