How can I add GET variables to end of the current url in php?

54,208

Solution 1

The simple way to it is:

$params           = array_merge( $_GET, array( 'test' => 'testvalue' ) );
$new_query_string = http_build_query( $params );

This doesn't guarantee that test will be at the end. If for some odd reason you need that, you can just do:

$params = $_GET;
unset( $params['test'] );
$params['test']   = 'testvalue';
$new_query_string = http_build_query( $params );

Note, however, that PHP query string parameter parsing may have some interoperability problems with other applications. In particular, PHP doesn't accept multiple values for any parameter unless it has an array-like name.

Then you can just forward to

( empty( $_SERVER['HTTPS'] ) ? 'http://' : 'https://' ) .
( empty( $_SERVER['HTTP_HOST'] ) ? $defaultHost : $_SERVER['HTTP_HOST'] ) .
$_SERVER['REQUEST_URI'] . '?' . $new_query_string

Solution 2

I created this simple PHP function based on Artefacto's Answer.

function addOrUpdateUrlParam($name, $value)
{
    $params = $_GET;
    unset($params[$name]);
    $params[$name] = $value;
    return basename($_SERVER['PHP_SELF']).'?'.http_build_query($params);
}
  • It updates the value if you are changing a existing parameter.
  • Adds up if it is a new value

Solution 3

function request_URI() {

    if(!isset($_SERVER['REQUEST_URI'])) {
        $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
        if($_SERVER['QUERY_STRING']) {
            $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
        }
    }
    return $_SERVER['REQUEST_URI'];
}

$_SERVER['REQUEST_URI'] = request_URI();

Courtesy: http://php.net/manual/en/reserved.variables.server.php example by LOL

This will give you the URL with respect to the root along with GET parameters.

In case you want it with respect to current directory, add the following.

$current_url = explode("/", $_SERVER['REQUEST_URI']);

$current_url = $current_url[end(array_keys($current_url))];

Solution 4

It may just be Joomla reasons, but I get the same value by using:

$currenturl = $_SERVER['REQUEST_URI'] ;

a lot less code.

Share:
54,208
zeckdude
Author by

zeckdude

Front and back-end Developer from Los Angeles, CA

Updated on March 06, 2020

Comments

  • zeckdude
    zeckdude about 4 years

    I am trying to add a few different GET variables to the url.

    I could easily do a header redirect to the current page url and then just add the $_GET['test'] in the url.

    My problem is that I have some GET variables that are in the url already. What I want to do is:

    • Check if there are any GET variables in the url

      • If there is not, then redirect to the current url with the new GET['test'] variable at the end of the url.

      • If there is, but there is no GET['test'] variable in the url, then keep those other GET values in the url and add the GET['test'] variable to end of the full url string

      • If there is, AND there is a GET['test'] variable in the url, then keep those other GET values in the url and exchange the GET['test'] variable value with the new value.

    How can I go about checking for all these conditions?

  • Artefacto
    Artefacto almost 14 years
    You know, it's considered good practice to accompany your downvotes with comments, otherwise the answer cannot be improved and the downvote doesn't serve any purpose – the exception being when the answer is clearly inflammatory. This is specially true if you're downvoting an alternative answer to yours; it just seems self-serving otherwise. As to your answer itself, I think you're taking the specifications of the OP too literally, but only he can tell that. Plus, it's really good practice to alter $GET (or $_SERVER).
  • Artefacto
    Artefacto almost 14 years
    I mean it's not a good practice (the editing windows has closed).
  • nathan
    nathan almost 14 years
    np, up voted now that you've expanded, was going to comment before but pulled away from pc before i could by kids ;)
  • MarcGuay
    MarcGuay over 11 years
    I believe that this will double-up on the GET vars. If the URL is currently domain.com/page.php?var=1 the final output of the "Then you can just forward to" code is domain.com/page.php?var=1?var=1&test=testvalue.
  • Jeffrey Nicholson Carré
    Jeffrey Nicholson Carré over 10 years
    replace REQUEST_URI with PHP_SELF
  • Ray S.
    Ray S. about 10 years
    why do you need: unset($params["test"]); isn't it replaced by the statement: $params["test"] = "testvalue";
  • M_R_K
    M_R_K about 9 years
    Check My Answer Below. It works like a charm. No GET var double ups. Simple function ;)
  • M_R_K
    M_R_K about 9 years
    Check my answer below. I created a simple function based on this answer
  • Olivier Van Bulck
    Olivier Van Bulck over 8 years
    REQUEST_URI and PHP_SELF are both wrong if you have redirects. The best way to do it is by defining the path with: explode('?', $_SERVER['REQUEST_URI'])[0] and add the query after it.