Skip to content
Advertisement

Remove querystring value on page refresh

I am redirecting to a different page with Querystring, say

header('location:abc.php?var=1');

I am able to display a message on the redirected page with the help of querystring value by using the following code, say

if (isset ($_GET['var']))
{

    if ($_GET['var']==1) 
    {
        echo 'Done';
    }
}

But my problem is that the message keeps on displaying even on refreshing the page. Thus I want that the message should get removed on page refresh i.e. the value or the querystring should not exist in the url on refresh.

Thanks in advance.

Advertisement

Answer

You cannot “remove a query parameter on refresh”. “Refresh” means the browser requests the same URL again, there’s no specific event that is triggered on a refresh that would let you distinguish it from a regular page request.

Therefore, the only option to get rid of the query parameter is to redirect to a different URL after the message has been displayed. Say, using Javascript you redirect to a different page after 10 seconds or so. This significantly changes the user experience though and doesn’t really solve the problem.

Option two is to save the message in a server-side session and display it once. E.g., something like:

if (isset($_SESSION['message'])) {
    echo $_SESSION['message'];
    unset($_SESSION['message']);
}

This can cause confusion with parallel requests though, but is mostly negligible.

Option three would be a combination of both: you save the message in the session with some unique token, then pass that token in the URL, then display the message once. E.g.:

if (isset($_GET['message'], $_SESSION['messages'][$_GET['message']])) {
    echo $_SESSION['messages'][$_GET['message']];
    unset($_SESSION['messages'][$_GET['message']]);
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement