如何告诉浏览器显示他的默认错误页面?

Her*_*uin 5 php browser http

为了包含正确的文件并在发生错误时显示错误页面,我有以下代码(非常简化):

$page = 'examplePage.php';
$page404 = '404.php';

if (file_exists($page))
{
    require($page);
}
else if (file_exists($page404))
{
    require($page404);
}
else
{
    // Tell the browser to display his default page
}

?>
Run Code Online (Sandbox Code Playgroud)

总结一下:

  • 如果我有该文件,我会将其包含在内。

  • 如果我没有该文件,我将包含错误文件。

  • 如果错误文件也不存在怎么办?

我希望它被呈现为浏览器的默认错误页面。

我已经使用 Internet Explorer 通过发送带有 HTTP 错误的空内容来实现这一点。问题是其他浏览器的行为不一样,它们都显示一个空白页面。

有没有办法告诉浏览器显示自己的错误页面?(不仅是 404,还有所有错误:304、500 等)

谢谢你。

编辑:我忘了告诉你,我可以完全控制我发送的标题和响应发送的内容。

编辑2:这是一些代码

// possible paths to retrieve the file
$possiblePaths = array(
    $urlPath,
    D_ROOT.$urlPath,
    D_PAGES.$urlPath.'.php',
    D_PAGES.$urlPath.'/index.php',
    $urlPath.'.php'
);

foreach ($possiblePaths as $possiblePath)
    if (file_exists($possiblePath) && !is_dir($possiblePath))
    {
        if (!is_readable($possiblePath))
        {
            Response::setCode(403); // calls the header(403)
            self::$filePath = self::getErrorPage(403);
        }
        else
            self::$filePath = $possiblePath;
        break;
    }

if (self::$filePath === null) // no file found => 404
{
    Response::setCode(404); // call the header(404)
    self::$filePath = self::getErrorPage(404); 
}


public static function _getErrorPage($code)
{
    if (is_readable(D_ERRORS.$code.'.php')) // D_ERRORS is the error directory, it contains files like 404.php, 403.php etc
        return D_ERRORS.$code.'.php';
    else
    {
        /*-------------------------------------------------*/
        /* Here i go if the error file is not found either */
        /*-------------------------------------------------*/

        if ($code >= 400)
            Response::$dieResponse = true; // removes all output, only leaves the http header
        return null;
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

这是我打印内容的时间:

    <?php
    if (self::$dieResponse)
    {
        self::$headers = array(); // no more headers
        self::$content = ''; // no more response
    }
    http_response_code(self::$code); // HTTP code
    foreach (self::$headers as $key => $value)
        header($key.': '.implode(';', $value)); // sends all headers
    echo self::$content;
    ?>
Run Code Online (Sandbox Code Playgroud)

编辑:这里有一些截图来解释我想要什么。

这是我在 IE 中得到的:

IE浏览器

这正是我想要的。

现在,在所有其他浏览器中,我有一个空白页面。我不想要一个空白页。

例如,我希望 Chrome 显示此内容: 铬合金

Jon*_*n M 1

如果您需要让它在任何输出之前显示默认的 404 页面,请执行以下操作:

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
Run Code Online (Sandbox Code Playgroud)

请参阅此处: http: //www.php.net/manual/en/function.header.php

因此,对于您的代码,您可以将其修改为:

$page = 'examplePage.php';
$page404 = '404.php';

if (file_exists($page))
{
    require($page);
}
else if (file_exists($page404))
{
    require($page404);
}
else
{
    header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
}

?>
Run Code Online (Sandbox Code Playgroud)

请注意以下警告,表明header必须在任何其他输出之前完成这些操作:

请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通 HTML 标记、文件中的空行还是从 PHP 发送。使用 include、require、函数或其他文件访问函数读取代码,并在调用 header() 之前输出空格或空行是一个非常常见的错误。使用单个 PHP/HTML 文件时也存在同样的问题。