Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript php中阻止缓存500内部错误页的问题_Javascript_Php_Error Handling - Fatal编程技术网

Javascript php中阻止缓存500内部错误页的问题

Javascript php中阻止缓存500内部错误页的问题,javascript,php,error-handling,Javascript,Php,Error Handling,我在完全正确配置500个内部服务器错误页面的行为时遇到问题 我有两个主要用例: 没有发送头 这里的修复很简单,只需抛出HTTP500错误而不更改url 部分页面错误 在本例中,一些html和http头已经发送到客户端。为了防止显示部分断开的页面,我输出javascript,该javascript会导致完全重定向到错误页面url/error.html。这是为了避免显示正常页面的一部分和错误页面的一部分,并通过移动到专用错误页面,明确即使错误消息最终显示的部分当前处于隐藏状态,结果html也不是最佳

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

我有两个主要用例:

没有发送头 这里的修复很简单,只需抛出HTTP500错误而不更改url

部分页面错误 在本例中,一些html和http头已经发送到客户端。为了防止显示部分断开的页面,我输出javascript,该javascript会导致完全重定向到错误页面url/error.html。这是为了避免显示正常页面的一部分和错误页面的一部分,并通过移动到专用错误页面,明确即使错误消息最终显示的部分当前处于隐藏状态,结果html也不是最佳的

不幸的是,当从专用错误页面回击时,原始错误页面会被缓存,javascript重定向会再次执行,即使在此期间错误已被修复

例如,如果在index.php中发送标题后出现错误,javascript将输出将用户重定向到/error.html。一旦到了那里,他们就可以返回主页,主页应该会很好,或者回击。当他们回击时,他们可能会得到一个缓存页面,该页面会将他们重新重定向到error.html。 index.php>error.html回击缓存的index.php>error.html

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

在下面的代码中,我尝试在url中添加错误哈希,以仅在第一次重定向时进行重定向,并在后续访问损坏的部分页面时开始60秒的刷新尝试周期。不幸的是,当我设置错误哈希和重定向,然后回击时,它转到index.php,而不是index.phperor,因此缓存的无限循环发生

如何优雅地处理部分第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中,让它成为页面上唯一显示的东西,否则页面上通常会有很多站点元素