When developing web applications in PHP, it is essential to handle errors effectively to ensure a smooth user experience and to promptly notify system administrators of any issues. In this tutorial, we will learn how to implement a basic exception handling system that will automatically send an email to the administrator when a 500 error occurs in our application.
Step 1: Configuration of the handler.php
File
Firstly, we will create a file named handler.php
that will contain our exception handling and email sending functions. This file can be included in all scripts of our application.
<?php // Set the email address to which the message will be sent $recipient = 'contact@email.com'; // Function to send an email function sendEmail($message, $recipient) { $subject = 'Error 500 on the website'; $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); // Use PHP's mail() function to send the email mail($recipient, $subject, $message, $headers); } // Function to handle exceptions function handleException($exception) { // Get information about the exception $message = 'An error has occurred on the website.' . "\n\n"; $message .= 'Error message: ' . $exception->getMessage() . "\n"; $message .= 'File: ' . $exception->getFile() . "\n"; $message .= 'Line: ' . $exception->getLine() . "\n"; // Send an email with the error information sendEmail($message, $GLOBALS['recipient']); // You can log the error somewhere else, like a log file // Show a generic message to the user (optional) echo 'An error has occurred. The administrator has been notified.'; // You can redirect the user to a custom error page if you wish // header('Location: error.php'); } // Set the custom exception handling function set_exception_handler('handleException'); // Generate a 500 error (for example, trying to access an undefined variable) echo $undefinedVariable; ?>
Step 2: Inclusion of the handler.php
File in Your PHP Scripts
Now, in each of your PHP scripts, include the handler.php
file at the beginning to enable exception handling and email sending.
// index.php <?php // Include the handler.php file include 'handler.php'; // Rest of the code for your main script // ... // Generate a 500 error (for example, trying to access an undefined variable) echo $undefinedVariable; ?>
// another_script.php <?php // Include the handler.php file include 'handler.php'; // Rest of the code for your other script // ... // Generate a 500 error (for example, trying to access an undefined variable) echo $anotherUndefinedVariable; ?>
If instead of sending by email, we want to display the error in the console:
<
pre class=”EnlighterJSRAW” data-enlighter-language=”generic” data-enlighter-theme=”” data-enlighter-highlight=”” data-enlighter-linenumbers=”” data-enlighter-lineoffset=”” data-enlighter-title=”” data-enlighter-group”>
$currentDate = date(‘Y-m-d H:i:s’);
$errorString = $currentDate . $message;
// Output log to console (docker)
file_put_contents(‘php://stdout’, $errorString . “\n”);
Conclusion:
With these simple steps, you have implemented a basic but effective PHP exception handling system that will automatically send emails to the administrator when a 500 error occurs in your application. Customize the functions according to your specific needs, and keep in mind that this is just a starting point; you can expand and enhance this functionality based on your project requirements.
You are now better equipped to maintain and manage your PHP application more effectively!
“`