在PHP中,最好的页面重新加载/重定向方式是什么,可以完全删除所有历史记录/缓存?应该使用哪些头信息?
发生了什么:
单击链接时,会设置GET参数并执行脚本。完成后,我想重定向和重新加载页面,而不带有GET参数。一开始似乎什么都没有发生,但按下F5后,更改才会出现。
我想要的:
重定向并重新加载页面,以便更改出现而无需按下F5。
header('Location: http://www.example.com/', true, 302);
exit;
参考:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
编辑:
只有在Cache-Control或Expires头字段指示的情况下,此响应才可缓存。
header("Cache-Control: no-cache");
header('Location: http://www.example.com/', true, 302);
exit;
- Mahdi Jazinifunction 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);
location
进行 HTTP 请求,则应确保尚未发送标头,而不是在它们已发送时使用后备。至少我是这样认为的。 - Kos试试这个:
echo '<script>document.location.replace("someurl.php");</script>';
这应该替换浏览器历史记录,但不影响缓存。
header('Location: http://example.com/path/to/file');
仅供参考,与SEO相关:
301会告诉搜索引擎在其索引中替换URL。因此,如果使用301将url1重定向到url2,则所有主要搜索引擎[Google、Yahoo + Bing]都会用url2替换url1。
302的工作方式不同。它表示该URL位于其他地址中临时
。
<?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();
?>
最安全的方法是使用头部重定向
header('Location: http://www.example.com/', true, 302);
exit;
但要注意,在将任何其他输出发送到浏览器之前,必须先发送它。