Log errors and show a safe page
Turn warnings into exceptions, log details privately and show visitors only a plain message.
PHP · Log errors and show a safe pageIntermediate
ini_set('display_errors', '0'); // never show errors to visitors
ini_set('log_errors', '1');
ini_set('error_log', __DIR__ . '/logs/php-error.log');
set_error_handler(function (int $no, string $msg, string $file, int $line) {
throw new ErrorException($msg, 0, $no, $file, $line);
});
set_exception_handler(function (Throwable $e) {
error_log(get_class($e) . ': ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
http_response_code(500);
echo 'Something went wrong. Please try again later.';
});errorexceptionlogging