On a php page, I'm displaying users the results of a query against the database. In addition, I give users the possibility to filter these results on 3 different parameters, so to only display the appropriate part from the database. I am wondering if my approach is safe enough, and whether there are easier or safer approaches achieving the same results.
More in-depth: I use the filters provider
, client
and phone
.
One provider can have multiple clients, one client can have multiple phones. The user fills their filters through a <form method='GET'>
, resulting in a Query String as follows:
?displayFilters[]=provider|1&displayFilters[]=client|&displayFilters[]=phone|3
In the case above, the user has decided to only display results for provider
'1', and for phone
type '3'. No filter was set for client
, hence after the |
is nothing. In my page, I analyse this query string to put the filter values in an appropriate array as follows:
if (isset($_GET['displayFilters'])) {
$displayFiltersTmp = $_GET['displayFilters'];
foreach ($displayFiltersTmp as $value) {
$displayFilters[substr($value, 0, strpos($value, "|"))] = substr($value, strpos($value, "|")+1);
}
foreach ($displayFilters as $key => $value) {
if ($displayFiltersURL == "")
$displayFiltersURL .= "displayFilters".urlencode("[]") . "=" . $key . urlencode("|") . $value;
else
$displayFiltersURL .= "&displayFilters".urlencode("[]") . "=" . $key . urlencode("|") . $value;
}
}
This way I can more easily manipulate the MySQL query for filtering the results as asked.
I also regenerate the original Query String for other use. I do this because the results that this page displays can be edited in a seperate page. Through my solution, I can pass these filters as a (couple of) GET-parameter(s), which can then be passed back to the original page displaying the results. Hence, after editing, the user is referred back to the displaying page with their filters still applied.
I am now wondering if this approach is 'safe' (I do the necessary checks before applying the filters to the MySQL-query), but also whether there are easier and/or more elegant solutions to this problem.
What I have now works, but I have concerns considering safety and perhaps stability.
Thanks for anyone willing to look into this! Kenneth Geets