I am trying to read get parameters in such a way that will not open up potential security issues.
What I was thinking was matching the request parameter explicitly to what I expect and then setting a default for anything that doesn’t match.
For example:
if ($_REQUEST['media'] == "video")
$sort = "video";
elseif ($_REQUEST['media'] == "audio")
$sort = "audio";
else
$sort = "both";
Is this enough or are further steps necessary?
What you mention is safe, but is overly verbose. Using an PHP’s array operations would let PHP handle the dirty work for you:
If this sort of superglobal parsing is common throughout your code, you could abstract this into a function that handles it for you (as many large PHP projects do).
As Gavin noted, it’s also a good idea to use the specific superglobal that you’re interested in (i.e.
$_GET,$_POST, or$_COOKIE) if at all possible. It might not seem important now, but some ugly bugs can manefest from naming conflicts will occur between the three superglobals (e.g.sortin$_COOKIEmay refer to the default sorting of search results, butsortin$_GETrefers to ascending or descending order).