Best Place to Filter Inputs?
<p>I am wondering where the best place to filter user submitted input is. In regards to filter, I am talking about <a href="http://php.net/manual/en/function.filter-var.php" rel="nofollow">filter_var</a> and <a href="http://www.php.net/manual/en/function.filter-input.php" rel="nofollow">filter_input</a>.</p>

<p>I've come up with three scenarios:<br/></p>

<ol>
<li>Filter data from POST/GET, and pass filter data to function which takes it as-is.</li>
<li>Take raw data from POST/GET, and pass as-is to function where the function filters it.</li>
<li>Filter data from POST/GET, and filter is a second time in the function.</li>
</ol>

<p>Each of these methods has its advantages and disadvantages. I was looking for some insight into which may be best or standard practice.</p>

<p>Method 1 passes sanitized data to the function, and thus functions can be smaller not having to sanitize everything coming in. The downfall is if any other place your function is called and the data isn't sanitized, this can lead to problems. This simply requires good coding practice to remember to sanitize everything before passing to a function.</p>

<p>Method 2 you will never have to worry about your function dealing with unsanitized data, but the functions will be bigger.</p>

<p>Method 3 is the safest, but is wasteful. More code is written, and data may be sanitized multiple times as it passes through possibly various functions, wasting CPU resources and time.</p>
