【问题标题】:How to tell the browser to display his default error page?如何告诉浏览器显示他的默认错误页面?
【发布时间】:2014-07-25 14:25:40
【问题描述】:

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

$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
}

?>

总结一下:

  • 如果我有文件,我会包含它。

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

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

我希望将其呈现为浏览器的默认错误页面

我已经使用 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;
    }
}
?>

这是我打印内容的时候:

    <?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;
    ?>

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

这是我在 IE 中得到的:

这正是我想要的。

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

例如,我希望 Chrome 显示以下内容:

【问题讨论】:

  • 如果您的错误页面本身丢失,您不会遇到比用户看到的更大的问题吗?
  • 请注意:浏览器与 404(或任何其他)错误页面无关。这一切都在服务器上。如果在服务器上看到错误情况,它将查找 .htaccess 中 ErrorDocument 下列出的相关错误页面,然后查找自定义文档(例如 /404.shtml),最后使用它自己的默认页面。浏览器只显示服务器决定发送的任何内容。
  • ceejayoz :没问题,在这种情况下,它只是意味着我没有针对此错误显示的特定文件。菲尔佩里:对,但错了。服务器规则,但浏览器有默认错误页面。这也是为什么 404 页面在不同浏览器中看起来不一样的原因。 Fred-ii-:我不能使用 apache 指令。他可能 404 不是 404 的东西(如果我的服务器上不存在该文件,并不意味着没有要加载的文件)。
  • 注意:不是所有的页面都是标准的浏览器,很多都是标准的服务器(例如 Apache、Nginx、lighttpd 等)
  • @GuilhermeNascimento 我不知道这个。如果你知道触发这些页面的方法,那我就全部打开了。

标签: php http browser


【解决方案1】:

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

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

请看这里: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");
}

?>

注意以下警告,header 的东西必须在任何其他输出之前完成:

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

【讨论】:

  • 实际上,我使用 ob 函数,因此在脚本结束之前不会打印任何内容。我已经试过了:只有 http 标头,没有别的,它不起作用。
  • 我刚刚仔细检查过了。它仅适用于 IE :/
  • 只有 IE 显示它的默认页面。其他浏览器没有(我试过 Firefox、Chrome、Safari、Opera)。
【解决方案2】:

也许你可以试试:

header('HTTP/1.0 404 Not Found');

【讨论】:

  • 最好使用header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");,因为您不知道使用了什么协议
【解决方案3】:

默认错误页面

如果内容为空白,Web 浏览器会显示默认错误页面,例如。创建一个空的 PHP 文件 (error.php) 并输入:

<?php
   $status = http_response_code();
   switch ($status) {
     case 404:
     case 500:
        exit;//terminate script execution
     break;
     ...
   }

在 .htaccess 中放置:

ErrorDocument 400 /error.php
ErrorDocument 500 /error.php

自定义错误页面

  1. 使用 HTTP 状态

    您可以使用http_response_code() 获取当前HTTP 状态、.htaccess 文件内容:

    ErrorDocument 400 /error.php
    ErrorDocument 401 /error.php
    ErrorDocument 403 /error.php
    ErrorDocument 404 /error.php
    ErrorDocument 500 /error.php
    ErrorDocument 503 /error.php
    

    页面错误.php:

    <?php
       $status = http_response_code();
       switch ($status) {
         case '400':
          echo 'Custom error 400';
         break;
         case '404':
          echo 'Custom error 404';
         break;
         ...
       }
    
  2. 使用 GET 参数

    ErrorDocument 400 /error.php?status=400
    ErrorDocument 401 /error.php?status=401
    ErrorDocument 403 /error.php?status=403
    ErrorDocument 404 /error.php?status=404
    ErrorDocument 500 /error.php?status=500
    ErrorDocument 503 /error.php?status=503
    

    页面错误.php:

    <?php
       $status = empty($_GET['status']) ? NULL : $_GET['status'];
       switch ($status) {
         case '400':
          echo 'Custom error 400';
         break;
         case '404':
          echo 'Custom error 404';
         break;
         ...
       }
    

相关: How to enable mod_rewrite for Apache 2.2

【讨论】:

  • 我不能,我已经在 PHP 脚本中,我正在使用路由器来获取文件。有时页面“example.php”会使用“scripts/example.php”中的脚本,放置ErrorDocuments不会让我处理什么是错误,什么不是。
  • 这并没有回答我的问题。我已经知道如何设置自定义ErrorDocument,我的问题是当404.php文件本身不存在时。
  • 虽然你还没有理解我的建议,所以我会制定另一个回应。
  • 我不想显示自定义页面,我想显示浏览器的默认页面。
  • 发送header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found'); 不带内容(例如,不带echo ...;)。注意:并非所有页面都是标准浏览器,很多都是标准服务器(例如 Apache、Nginx、lighttpd 等)
【解决方案4】:

不久前我问过类似的问题 Access apache errordocument directive from PHP

结果要么将用户重定向到通用 404 页面(因此地址更改)Header("Location: $uri_404");,要么卷曲您自己的 404 页面并回显它,如下所示:

Header('Status: 404 Not Found');

$uri_404 = 'http://'
    . $_SERVER['HTTP_HOST']
    . ($_SERVER['HTTP_PORT'] ? (':' . $_SERVER['HTTP_PORT']) : '')
    . '/was-nowhere-to-be-seen';
$curl_req = curl_init($uri);
curl_setopt($curl_req, CURLOPT_MUTE, true);
$body = curl_exec($curl_req);
print $body;
curl_close($curl_req);

代码归功于@RoUS

【讨论】:

  • 抱歉进一步阅读您要求使用默认的“浏览器”404 页面。我不认为这样的事情存在(IE除外)。
猜你喜欢
  • 2011-01-19
  • 1970-01-01
  • 2014-11-13
  • 2020-06-30
  • 2012-11-01
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多