在 PHP 中重定向/重新加载页面的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1571973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:08:05  来源:igfitidea点击:

best way redirect/reload pages in PHP

phpredirectreload

提问by jorgen

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

在完全删除所有历史记录/缓存的 PHP 中重新加载/重定向页面的最佳方法是什么?我应该使用哪些标题?

What happens:

发生什么了:

On clicking a link, get-parameters is set and a script is executed. When finished, I want to redirect and reload the page without the get-parameters. At first, it looks like nothing has happened, but when pressing F5, the changes appear.

单击链接时,将设置 get-parameters 并执行脚本。完成后,我想在没有获取参数的情况下重定向并重新加载页面。乍一看,好像什么都没发生,但是按F5,变化就出现了。

What I want:

我想要的是:

Redirect and reload so the changes appear without pressing F5.

重定向并重新加载,以便无需按 F5 即可显示更改。

回答by Ismael

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

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

参考: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.

此响应仅在 Cache-Control 或 Expires 标头字段指示时才可缓存。

回答by Phill Pafford

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);

回答by rahul

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.

重新加载页面并强制其不从缓存中取出的最佳方法是将随机 ID 或时间戳作为查询字符串附加到 url 的末尾。它使请求每次都是唯一的。

回答by jerjer

Try this:

尝试这个:

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

This should replace browser history but not cache.

这应该替换浏览器历史记录而不是缓存。

回答by knittl

header('Location: http://example.com/path/to/file');

回答by Kiran P.

just for information, related to SEO:

仅供参考,与 SEO 相关:

301 would tell search engine to replace url in their index. so if url1 is redirecting to url2 with 301, all major search engine [google, yahoo + bing] would replace url1 with url2.

301 会告诉搜索引擎替换其索引中的 url。因此,如果 url1 使用 301 重定向到 url2,所有主要搜索引擎 [google, yahoo + bing] 都会将 url1 替换为 url2。

302 works in different way. It says the url is located temporarilyin some other address.

302 以不同的方式工作。它说该 url 位于temporarily其他某个地址。

see this post

看到这个帖子

回答by warfish

<?php
header('Cache-Control: no-store, private, no-cache, must-revalidate');     // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0, max-stale = 0', false);  // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');                  // Date in the past  
header('Expires: 0', false); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header ('Pragma: no-cache');
header("Location: https://example.com", true, 302);
exit();
?>

回答by clops

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.

但请注意,它必须在任何其他输出发送到浏览器之前发送。