Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8786539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:39:24+00:00 2026-06-13T21:39:24+00:00

Im using the $_GET[variable from link] – way to pass the options the user

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:39:26+00:00Added an answer on June 13, 2026 at 9:39 pm

    With your example code, each query-string parameter you access through $_GET, such as $_GET['page'], you directly append to a $header variable, such as $header = '&page=' . $_GET['page'];.

    Since each key looks like it has “rules”, such as the id can’t be 0, 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:

    if (!isset($_GET['id']) || ((int)$_GET['id'] == 0)) {
        // invalid id; no need to process further.
        header('Location: error.php');
        die();
    }
    
    // set the ID
    $queryString = 'id=' . (int)$_GET['id'];
    
    // process the current page
    $queryString .= '&page=' . ((empty($_GET['page']) || ((int)$_GET['page'] <= 0)) ? 1 : $_GET['page']);
    
    // set the order, if available
    if (!empty($_GET['order'])) {
        $queryString .= '&order=' . $_GET['order'];
    }
    
    // redirect
    header('Location: topic.php?' . $queryString);
    die();
    

    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:

    // define the list of "keys" and their "rules"
    $keys = array(
        'id'    => array('required' => true, 'min' => 1),
        'page'  => array('required' => false, 'min' => 1, 'default' => 1),
        'order' => array('required' => false)
    );
    
    $query = '';
    foreach ($keys as $key => $rules) {
        // get the value of the key from the query-string, if set
        $value = (empty($_GET[$key]) ? null : $_GET[$key]);
    
        // determine if the value is valid or not
        $valid = (empty($value) || (isset($rules['min']) && ((int)$value < $rules['min']))) ? false : true;
    
        if ($rules['required'] && !$valid) {
            // required key but invalid value =[
            header('Location: error.php');
            die();
        } else if (!$rules['required'] && !$valid) {
            if (!empty($rules['default'])) {
                // the key is not set but has a default value
                $value = $rules['default'];
            } else {
                // the key is not required, is not set, and has no default value; skip it
                continue;
            }
        }
    
        // append the key/value to the query
        $query .= (($query != '') ? '&' : '') . $key . '=' . $value;
    }
    
    header('Location: topic.php?' . $query);
    die();
    

    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 min is 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 to error.php. If the key is not required and the value is invalid but there is a default value 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, min and required. You could build upon this to add additional rules such as max to set the highest value, or in_list to define a list of valid values (such as for order, you could have 'in_list' => array('asc', 'desc') and then check it with in_array($value, $rules['in_list'])). It has potential, but is only limited to your needs!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JSON to get some values into a variable from an external source.
I'm trying to extract the price from the bellow html page/link using php cURL
I'm using a GET variable and people get to it by the following URL:
I am using boost::format variable to get elapsed time in seconds boost::posix_time::time_duration total_time =
I have a total variable that I update with a refresh get request using
Using the vxWorks API symFind() we can get the address of a global variable
I'm trying to get the value of a variable using SQL select queries. Nothing
I have a variable when i write its value using document.write() i don't get
I'm using console.log(variable) and firebug to examine javascript code. Occasionally, I get [object][object] as
How do I create dynamic images using php which depend of $_GET['x'] variables?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.