Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell the browser to display his default error page?

Tags:

browser

http

php

In order to include the right file and display an error page if 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
}

?>

To summarize :

  • If I have the file, I include it.

  • If I don't have the file, i include the error file.

  • What if the error file does not exist too ?

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

I already achieved this with Internet Explorer by sending an empty content with the HTTP error. The problem is that the other browsers don't act the same, 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 you.

Edit : I forgot to tell you that I have the complete control on the headers I send and on the content sent in response.

Edit 2 : here is some code

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

And here is when I print the content :

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

Edit : here are some screenshots to explain what I want.

This is what i've got in IE :

Internet Explorer

This is exactly what i want.

Now, in all the other browsers, I've got a blank page. I don't want a blank page.

I want, for example, Chrome to display this : Chrome

like image 850
FrancoisBaveye Avatar asked Oct 01 '22 15:10

FrancoisBaveye


1 Answers

Default error pages

Web Browsers shows default error pages if content is blank, eg. create a empty PHP file (error.php) and put this:

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

In .htaccess put:

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

Custom error pages

  1. Using HTTP status

    You can use http_response_code() for GET current HTTP status, .htaccess file content:

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

    Page error.php:

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

    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
    

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

Related: How to enable mod_rewrite for Apache 2.2

like image 79
Guilherme Nascimento Avatar answered Oct 05 '22 22:10

Guilherme Nascimento