How do I tell the browser to display the default error page?

To include the correct file and display the error page when an error occurs, I have the following code (very simplified):   

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

?>

Summarizing:

  • If I have a file, I include it.

  • If I do not have the file, I include the error file.

  • What if the error file also does not exist?

I would like it to display as the default error page of the browser .

I already achieved this in Internet Explorer by sending empty content with an HTTP error. The problem is that other browsers do not work the same way, they all display a blank page .

Is there any way to tell browsers to display their own error page? (not only 404, but all errors: 304, 500, etc.)

Thank.

: , , .

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:

Internet explorer

, .

. .

, , Chrome : Chrome

+4
4

- , , . PHP (error.php) :

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

.htaccess put:

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

  • 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
    

    error.php:

    <?php
       $status = http_response_code();
       switch ($status) {
         case '400':
          echo 'Custom error 400';
         break;
         case '404':
          echo 'Custom error 404';
         break;
         ...
       }
    
  • 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
    

    error.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;
         ...
       }
    

: mod_rewrite Apache 2.2

0

apache errordocument PHP

Upshot 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

0

404, :

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. , , , header(). PHP/HTML.

-1

, :

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

Source: https://habr.com/ru/post/1543284/


All Articles