PHP Error Page

Continuing the discussion from Custom php error pages?:

I have been searching around on how to do this.
As things stand if I make an error in my code, maybe just a typo, missing a ; or a , or whatever, the screen displays plain white, the error gets logged in the error.log file.
I thought it would be better for users if it displayed some sort of Error Screen. I don’t want it to display details about the error, that is not their concern (its mine), just something other than and less confusing than a blank page, like “Sorry, there was an error, the site is not completely dead, honest.” maybe not that exactly, but you get the idea.
I have yet to find out how this is done.

My problem as a coder with pretty error screens in general is that they dont actually give you the information necessary to fix the error. They’re pretty screens designed for production environments that are basically “oops, something went wrong”.

Yup, it looks ugly to see a bunch of error messages show up on my dev environment, but they tell me exactly what happened - I missed a semicolon on line 54. Oh. Okay. let me go fix that.

1 Like

On a production website you really shouldn’t display errors to the end user, what I do is send an email to myself if an error should occur. You can also create a private log if you don’t want to go the email route. Sending an error message to the user does two things in my opinion. It might give the end-user the impression that your a bumbling fool who doesn’t know how to code or give the hacker a foothold into you website(s).

As I said, it is more for the benefit of users, if the screen goes blank, they may be confused by that, maybe think the page or site does not exist or something. I don’t want to display the error details to them. I will get that info from the log and fix it. They will just see that there is some error, but don’t worry, try another page or visit again later when its fixed.

That’s the sort of thing I want. I did read something a while back about making errors send a email, but can’t find it now.
Showing something other than a blank page is better IMO. And if I am alerted by it by Email, that’s good, the sooner I can fix it.

This link may be helpful:

http://php.net/manual/en/book.errorfunc.php

My approach to handling errors

  1. Set error_log( ‘My-Project-Path’ );
  2. Set error_reporting(-1); // maximum
  3. Set ini_set(‘display_errors’, 0); // do not show on screen
  4. Monitor error log file and modify script to prevent further errors.

You might want to consider generating a 500 Internal Server Error and show the user a generic error message, below is what I use to generate one if the database server doesn’t want to connect (used in conjunction with a try-catch block)

header('HTTP/1.1 500 Internal Server Error');
            echo '
            <h1>AWOL Database</h1>
            <p>Our appologies, the site\'s database appears to have gone AWOL!</p>
            <p>A search party has been dispatched to locate it and drag it back!</p>
            ';
            
            // also notify server operator, maybe?
            exit();

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.