14

How can redirect the user back to the same page with the existing Query Strings but have 1 added/modified like "page".

I suppose 1 method is:

  1. parse the $_SERVER['QUERY_STRING'] into an array
  2. if page exists in the array, modify the value, else add it
  3. use http_build_query to get the query string to append to $_SERVER['PHP_SELF']

but is there a better/more direct way?

1

3 Answers 3

27

Just to make sure you know about it, use parse_str to parse your query string:

<?php
parse_str($_SERVER['QUERY_STRING'], $query_string);
$query_string['page'] = basename($_SERVER['PHP_SELF']);
$rdr_str = http_build_query($query_string);
Sign up to request clarification or add additional context in comments.

2 Comments

oh I was looking at that, but missed out the part where I can pass an array
Note: Requies the PECL library pecl_http >= 0.21.0 for http_build_query.
6

Using Jim Rubinstein's answer, I came up with a useful function that I thought I might share:

  function modQuery($add_to, $rem_from = array(), $clear_all = false){
  if ($clear_all){
     $query_string = array();
  }else{
     parse_str($_SERVER['QUERY_STRING'], $query_string);
  }
  if (!is_array($add_to)){ $add_to = array(); }
  $query_string = array_merge($query_string, $add_to);
  if (!is_array($rem_from)){ $rem_from = array($rem_from); }
  foreach($rem_from as $key){
     unset($query_string[$key]);
  }
  return http_build_query($query_string);
  }

For example: <a href="?<?=modQuery(array('kind'=>'feature'))?>">Feature</a>

Comments

0

If you want to receive both an absolute or a relative URI and replace one query string parameter (or even more), preserving whatever other parameter in the query string, like this:

// Get https://example.com?page=asdlol
echo uri_with_extra_params('https://example.com', [
    'page' => 'asdlol',
]);

// Get https://example.com?page=asdlol
echo uri_with_extra_params('https://example.com?page=old', [
    'page' => 'asdlol',
]);

// Get https://example.com?test=toast&page=asdlol
echo uri_with_extra_params('https://example.com?test=toast', [
    'page' => 'asdlol',
]);

// Get /homepage/?page=asdlol
echo uri_with_extra_params('/homepage/', [
    'page' => 'asdlol',
]);

To reach the above results, where the original URI is kept as-is (relative or absolute), just replacing the very specific query string parameters, you can use this function:

/**
 * Get an URI as-is, forcing one query string parameter or more. 
 *
 * @param $uri string  URI relative or absolute, with or without query string
 * @param $extra array Associative query string parameters to be added or replaced.
 * @return             URI relative or absolute, with the desired query string parameters
 */
function uri_with_extra_params(string $uri, array $extra): string
{
    // Get the URI and the query string.
    $url_parts = explode('?', $uri, 2);
    $url_without_query_string = $url_parts[0];
    $query_string_raw = $url_parts[1] ?? '';

    // Get the query string array data.
    $query_string_data = [];
    parse_str($query_string_raw, $query_string_data);

    // Force your new parameter(s).
    $query_string_data_new = array_replace($query_string_data, $extra);

    // Return the final URI in the desired language.
    return $url_without_query_string . '?' . http_build_query($query_string_data_new);
}

Example to rewrite the current request URI and forcing a specific page "p":

// If the page was http://localhost/?something=1&page=1,
//    get the page http://localhost/?something=1&page=2
$new_page = uri_with_extra_params($_SERVER['REQUEST_URI'], [
    'page' => '2',
] );

I like this solution because it keeps the rest of the query string as-is, and it works for both relative and absolute URIs.

1 Comment

I've added this solution since other solutions does not work with relative URIs, and because all other solutions replaces the whole query string; instead I like to keep the original query string as-is when possible, just replacing the specific needed argument. Moreover, sometime you need to replace more than 1 parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.