views:

984

answers:

7

Hi,

Whats the best way to reload/redirect a page in PHP which completly removes all history/cache? Which headers should I use?

The page today:

While clicking on a link, get-parameters is set and a script is running. When finished, I want to redriect and reload the page without the get-parameters. At first, it looks like nothing has happen, but when pressing F5, the changes appear.

What I want:

Redirect and reload so the changes appear without pressing F5.

A: 

Try this:

echo '<script>document.location.replace("someurl.php");</script>';

This should replace browser history but not cache.

jerjer
+7  A: 

The best way to reload a page and force it to be not taken from the cache will be to append a random id or timestamp to the end of the url as querystring. It makes the request unique each time.

rahul
It can be the best way, it's strange to do this.Imagine: www.google.com.br?randomId=12381289371The best way should be send appropriated headers.
Ismael
+1  A: 
header('Location: http://example.com/path/to/file');
knittl
+5  A: 
header('Location: http://www.example.com/', true, 302);
exit;

Ref: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

edit:

This response is only cacheable if indicated by a Cache-Control or Expires header field.

Ismael
302 is correct. Congratulations.
Cesar
A: 

Thanks for all answers, but the do not seems to work for me...

jorgen
I'd like to know some details why it doesn't work for you.This is the way to do it. If it doesn't work it may be because some detail in your program.
Ismael
A: 

The safest way is to use a Header Redirect

header('Location: http://www.example.com/', true, 302);
exit;

But beware, that it has to be sent BEFORE any other output is sent to the browser.

clops
+1  A: 
function redirect($url) {
    if(!headers_sent()) {
        //If headers not sent yet... then do php redirect
        header('Location: '.$url);
        exit;
    } else {
        //If headers are sent... do javascript redirect... if javascript disabled, do html redirect.
        echo '<script type="text/javascript">';
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        echo '<noscript>';
        echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
        echo '</noscript>';
        exit;
    }
}

// How to use
$url = "www.google.com";
redirect($url);
Phill Pafford