Skip to content
Advertisement

PHP hide fatal error message and redirect

If the following code returns a fatal error, I get a message on the page I don’t want users to see. I want to hide the fatal error message and redirect the user to another page:

$jinput = JFactory::getApplication()->input;

To hide the error message, I just added:

error_reporting(E_NONE);

But how do I catch and redirect a fatal error like this?

Advertisement

Answer

Basicaly, you can’t “catch” fatal errors because they stop program execution. But there is one method to handle them. Register your own shutdown handler and check if last error was fatal.

<?php
function shutdownHandler()
{
    $error = error_get_last();
    if ($error['type'] == E_ERROR) {
        your code goes here;
    }
}
register_shutdown_function('shutdownHandler');

Demo: https://eval.in/137869

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement