4

PHP is famous for displaying ugly error messages, though they are useful at times. I know I can hide error messages with

error_reporting(0);

That's fine, but it leaves the user completely in the dark as to what is going on. The page has stopped working and they don't know why. How can I display a simple message along the lines of "Sorry, there is an error. Please send an e-mail to the webmaster"?

It would be the same message for all errors, and I'm thinking of maybe popping up a javascript alert, but open to any other ideas.

2

5 Answers 5

4

Implement an error and exception handler

You need to write a custom error handler like this. As you can see at the bottom, I am introducing a FATAL error. Here PHP does not spit any ugly error messages as you have quoted. It would just print Some Error Occured. Please Try Later.

<?php

set_error_handler( "log_error" );
set_exception_handler( "log_exception" );
function log_error( $num, $str, $file, $line, $context = null )
{

    log_exception( new ErrorException( $str, 0, $num, $file, $line ) );
}

function log_exception( Exception $e )
{
    http_response_code(500);
    log_error($e);
    echo "Some Error Occured. Please Try Later.";
    exit();
}

error_reporting(E_ALL);
require_once("texsss.php");// I am doing a FATAL Error here
Sign up to request clarification or add additional context in comments.

1 Comment

That looks good. Just one problem: the error doesn't show up in the normal error log. (It works if I comment out the custom error handler.) Is it logging it somewhere else?
2

There are some rules that should apply to production servers:

  1. Never show them the original PHP error message! Set display_errors = off.
  2. Log those errors. Set log_errors = on and define a valid log target in error_log.
  3. Monitor the error log, and act upon it. :)

The handling of errors to the user side has been sufficiently answered by the others.

1 Comment

I can't claim to have always obeyed those rules in the past, but I intend to from now on. That's why I asked the question.
1

You can write custom error handler that does it

http://php.net/manual/en/function.set-error-handler.php

Comments

1

This is typically done via Exceptions. If something goes awry, you'd throw an Exception and then "handle it" with an exception handler.

function exception_handler($exception) {
  echo "Oops! Something went wrong! We're looking into it!";
}

set_exception_handler('exception_handler');

throw new Exception('Uncaught Exception');

You can also catch a number of fatal errors by using register_shutdown_function.

set_error_handler might also tickle your fancy.

Comments

0

User custom error handling set_error_handler

and exception handler set_exception_handler

and there you can do what ever you like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.