I really hate global variables - maybe its the C# programmer in me but when I'm working in PHP I grit my teeth every time I have to do something like this:
$strUsername = $_GET['username'];
Yes, I'm grossly oversimplifying it and yes yes I sanitize all of this properly. In fact, for the framework that I built, all of the superglobals are grabbed almost at the beginning of execution and are dependency-injected from there on out.
I ran across this function in the PHP manual (you truly learn something new every day): filter_input_array().
So now, technically, I can do this instead of grabbing everything from the GET superglobal:
$GETdata = filter_input_array(INPUT_GET);
.... and so on and so forth with the others like POST, REQUEST, etc. My question is: should I use filter_input_array and so avoid the scourge of superglobals, or is there some reason to stick with them and forget about using the filter_input functions? What is everyone else's experience with this?
EDIT: I forgot one thing - the filter_input functions are blind to any script-level modifications you make to the superglobals so if I do: $_GET['cheese'] = 'puff';
trying to do filter_input(INPUT_GET, 'cheese');
later will return null. This is fine since I dependency inject everything but it could catch somebody off guard later, if they are unaware.
foo()
and sanitize them there, but what ifbar()
needs them as well? Do you re-sanitize? – LemoineRequest
class and become happy then ;-) – Skillet