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

发布于 2024-08-08 07:46:26 字数 205 浏览 2 评论 0原文

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

发生了什么:

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

我想要的:

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

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

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.

What I want:

Redirect and reload so the changes appear without pressing F5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

[旋木] 2024-08-15 07:46:26
header('Location: http://www.example.com/', true, 302);
exit;

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

编辑:

此响应仅可缓存,如果
由 Cache-Control 或
标头字段过期。

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.

瞄了个咪的 2024-08-15 07:46:26
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);
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);
电影里的梦 2024-08-15 07:46:26

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

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.

感情洁癖 2024-08-15 07:46:26

试试这个:

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

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

Try this:

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

This should replace browser history but not cache.

叫思念不要吵 2024-08-15 07:46:26
header('Location: http://example.com/path/to/file');
header('Location: http://example.com/path/to/file');
情泪▽动烟 2024-08-15 07:46:26

仅供参考,与 SEO 相关:

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

302 的工作方式不同。它表示该网址暂时位于其他地址。

查看这篇文章

just for information, related to 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.

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

see this post

豆芽 2024-08-15 07:46:26
<?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();
?>
<?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();
?>
不醒的梦 2024-08-15 07:46:26

最安全的方法是使用标头重定向,

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文