Im using the $_GET[variable from link] – way to pass the options the user selected on my webpage, for example the post per page or the post-oder.
I was thinking to write a simple variable which would give me out the options the user selected so I could use it in my header-redirects:
if ((isset($_GET['id'])) AND($_GET['id'] != 0 )) {
$topic_id = (int)$_GET['id'];
$header = 'Location: topic.php?id='.$topic_id;
}
else {
header('Location: error.php');
die();
}
if ((!isset ($_GET['page'])) OR ((int)$_GET['page'] == NULL)) {
header('Location: topic.php?id='.$topic_id.'&page=1');
die();
}
else {
$page = (isset($_GET['page']) AND (int)$_GET['page'] > 0) ? (int)$_GET['page'] : 1;
$header = $header.'&page='.$page;
}
if(isset($_GET['order']) AND $_GET['order'] != NULL) {
$order = $_GET['order'];
$header = $header.'&order='.$order;
}
and so on.
So I got now the options the user selected, but if I want to change one of them for a special redirect, I would need to write a string-replace-function. My question is: Is there a better way to handle many $_GET options and/or missing options? Is there any good practice? Sorry if this is a simple question.
With your example code, each query-string parameter you access through
$_GET, such as$_GET['page'], you directly append to a$headervariable, such as$header = '&page=' . $_GET['page'];.Since each key looks like it has “rules”, such as the
idcan’t be0, looping through a list of keys would be do-able, however, you’d also need to keep a list of rules to evaluate and it wouldn’t be worth it (unless you had a lot of keys, then I could see a good benefit).Instead, you could re-order and clean-up your code to build the full query-string and then worry about actually redirecting:
While this still requires manual work for each key, it’s easier to manage (in my opinion) and the actual URL/redirect is handled in only a single place. This is also a method I would use on smaller projects, ones that aren’t expected to change (much) or have a dynamic/growing list of parts.
If the above method is too tedious (which can easily become the case when you have too many query-string parameters/rules), creating a list of keys and their rules would become the preferred method. Here’s a quick thrown-together (and not tested) sample of this method:
The above is a skeleton of a key/rule-based system. The rules are very minimal and define if a key is required and what it’s minimum value is (if a
minis set, it assumes it’s an integer and casts it via(int)). If the key is required and not set, or not a valid value, it immediately fails and redirects toerror.php. If the key is not required and the value is invalid but there is adefaultvalue set, it will use that value; otherwise it will skip that key. After all value-processing is done, it appends the key/value to the query and continues on it’s way.I say that it is a “skeleton” system because it only defines two rules,
minandrequired. You could build upon this to add additional rules such asmaxto set the highest value, orin_listto define a list of valid values (such as fororder, you could have'in_list' => array('asc', 'desc')and then check it within_array($value, $rules['in_list'])). It has potential, but is only limited to your needs!