4

If I have the url mysite.com/test.php?id=1. The id is set when the page loads and can be anything. There could also be others in there such as ?id=1&sort=new. Is there a way just to add another to the end without finding out what the others are first then building a new url? thanks.

3 Answers 3

24

As an alternative to Kolink's answer, I think I would utilize http_build_query(). This way, if there is nothing in the query string, you don't get an extra &. Although, it won't really make a difference at all. Kolink's answer is perfectly fine. I'm posting this mainly to introduce you to http_build_query(), as you will likely need it later:

http_build_query(array_merge($_GET, array('newvar'=>'123')))

Basically, we use http_build_query() to take everything in $_GET, and merge it with an array of any other parameters we want. In this example, I just create an array on the fly, using your example parameter. In practice, you'll likely have an array like this somewhere already.

Sign up to request clarification or add additional context in comments.

6 Comments

theres no harm in inserting get variables straight into html right?
@AndyLobel, Of course there is harm! You should be using htmlspecialchars() on any PHP variable you echo out to a page. Otherwise, you open yourself up to XSS. You don't know what's in that variable, unless you can guarantee it is strictly numeric or something. Even then, out of caution, I use htmlspecialchars() every time.
ooo right when you say any do you mean like ones that iv'e manually set as well!? thanks ;p
@AndyLobel, Yes, you should be doing this with everything that is ending up on a page.
@AndyLobel, In that case, technically it won't make a difference. But in practice, you'll rarely have a variable where $a = 'b'.
|
3
"?".$_SERVER['QUERY_STRING']."&newvar=123";

Something like that.

1 Comment

cheeeerss ;p even tho remeba when the page reloads if you try and click the same link again it will go ?id=1&newvar=123&newvar=123
1

Use this function: https://github.com/patrykparcheta/misc/blob/master/addQueryArgs.php

function addQueryArgs(array $args, string $url)
{
    if (filter_var($url, FILTER_VALIDATE_URL)) {
        $urlParts = parse_url($url);
        if (isset($urlParts['query'])) {
            parse_str($urlParts['query'], $urlQueryArgs);
            $urlParts['query'] = http_build_query(array_merge($urlQueryArgs, $args));
            $newUrl = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '?' . $urlParts['query'];
        } else {
            $newUrl = $url . '?' . http_build_query($args);
        }
        return $newUrl;

    } else {
        return $url;
    }
}

$newUrl = addQueryArgs(array('add' => 'this', 'and' => 'this'), 'http://example.com/?have=others');

Comments

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.