繁体   English   中英

防止在PHP中缓存500个内部错误页面的问题

[英]Problems with preventing caching of 500 internal error page in php

我在完全正确配置500个“内部服务器错误”页面的行为时遇到问题。

我有两个主要的用例:

未发送标题

此处的修复很简单,仅抛出http 500错误而不更改url。

部分页面错误

在这种情况下,一些html和http标头已经发送到客户端。 为了防止显示部分损坏的页面,我输出了导致完全重定向到错误页面URL /error.html的javascript。 这是为了避免显示正常页面的一部分和错误页面的一部分,并且通过移动到专用的页面,即使当前隐藏了错误消息最终显示的部分,也可以清楚地看到生成的html不是最佳的html。错误页面。

不幸的是,当从专用错误页面上单击“返回”时,原始错误页面将被缓存,并且即使在此期间已修复错误的情况下,也会再次执行javascript重定向。

例如,如果在index.php中发送头后出现错误,则javascript将输出将用户重定向到/error.html的输出。 到达那里后,他们可以返回首页(应该没问题),或回击。 当他们回击时,他们可能会得到一个缓存的页面,该页面将它们重新重定向到error.html。 index.php> error.html(回击)已缓存的index.php> error.html

避免这种情况的理想方法是什么?

在下面的代码中,我尝试在URL中添加#error哈希值以仅第一次重定向,并且在随后访问断开的部分页面时,开始60秒的刷新尝试周期。 不幸的是,当我设置#error哈希值和重定向,然后进行回击时,它将到达index.php,而不是index.php#error,因此发生了缓存的无限循环。

如何优雅地处理500页的部分错误?

这是我的php自定义错误处理程序中的代码,它会导致上述行为:

function showErrorPage() {
    if (headers_sent()) {
    // This is complicated due to the infastructure tending to have already sent headers, ...
    // ...requiring a tuned javascript redirection in many instances.

        // Output the following as html.
        ?>
        <meta http-equiv="Cache-control" content="no-cache, no-store">
        <script type='text/javascript'>
        var currentHash = location.hash;
            // UNFORTUNATELY, HERE THE HASH NEVER SHOWS AS SET!
        if(!currentHash){ // If hash wasn't already set...
            location.hash = '#error';
            location.href = 'error.html'; // Redirect once.
        } else { // Otherwise, the hash was already set, they were redirected once but came back.
            // So just display this page for a time but refresh on a slow schedule.
            setTimeout(function(){
                location.reload(true); // Non-caching refresh.
            }, 60*1000); // Reload after 1 min delay
        }
        </script>
        <?php
    } else {
        // No headers sent, so set the right headers
        header("HTTP/1.1 500 Internal Server Error");
        header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
        header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    }
    // Regardless, include the visible error output and throw exit with error.
    include(WEB_ROOT.'error.html');
    exit(1);
}

我找到了一个有用的解决方案,如下所示:

通过javascript,隐藏页面的所有部分,并在div中将其替换为错误消息。 具体来说,我直接写了html,将其设置为隐藏样式,克隆,删除了body元素的所有子元素,然后将error div附加到body上,使它成为页面上唯一显示的内容,否则通常上面有很多网站元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM