Skip to main content

PHP: Error Reporting

As previously mentioned, this post deals with Error Reporting. The class itself is pretty simple. You have the main() method that creates the error page and you can also call the error() method from another class to email and store the message.

In both instances the previous and current pages are recorded for diagnosis.

If you take a look at the storeErrorMessage() method, you'll notice how the error message is constructed and stored into the $error variable. The sendErrorToWebmaster() method is then called.

In another file, instantiate the class. Where an error is likely to occur, construct the $error variable and call the error() method i.e. $this->_error_reporting_obj->error($error);

<?php
/* *
* AUTHOR/LICENSE HOLDER INFORMATION *
* @author Concentrated Software Solutions *
* @programmer Dino Cajic *
* */
chdir(__DIR__); require_once("../config/config.php");
chdir(__DIR__); require_once("../database/db_connect.php");
/**
* Class ReportError
*
* Sends an error to the webmaster
*/
class ReportError {
/** @var object Config | Stores the configuration information */
private $_config_obj;
/** @var object DbConnect | Stores the database connection object */
private $_db_obj;
/** @var string mysqli() | Stores the connection to the database */
private $_mysqli;
/** @var string $_previous_page | Stores the previous page if existed */
private $_previous_page;
/** @var string $_current_page | stores the current page */
private $_current_page;
/**
* Instantiates the classes
* Establishes a connection to the database
* Stores the previous page if available and the current page
*/
public function __construct() {
$this->_config_obj = new Config();
$this->_db_obj = new DbConnect();
$this->_mysqli = $this->_db_obj->mysqli();
$this->_previous_page = (isset($_SERVER['HTTP_REFERER'])) ? htmlspecialchars($_SERVER['HTTP_REFERER']) : "Directly Typed In";
$this->_current_page = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$this->_current_page = htmlspecialchars($this->_current_page);
}
/**
* Displays the 404 page when an error occurs
*
* @return void
*/
public function main() {
?>
<div class="container">
<h1>404 : It looks like you found our page. Congratulations!</h1>
<h4>P.S. Don't worry. We've already contacted our web-chimp to correct this issue.</h4>
<img src="img/web_chimp.jpg" />
<?php
$this->sendErrorToWebmaster();
$this->displayNotifiedMsg();
$this->storeErrorMessage();
?>
</div>
<?php
}
/**
* Called from other classes when an error is triggered.
*
* @param $error_message
* @return void
*/
public function error($error_message) {
$this->sendErrorToWebmaster($error_message);
$this->storeErrorMessage();
}
/**
* Emails the IT person letting them know that an error page was accessed
*
* @param string $error_message
* @return void
*/
private function sendErrorToWebmaster($error_message = "Page loading problem") {
$headers = 'From: ' . Config::$_WEBMASTER_EMAIL . "\r\n";
$headers .= "MIME-version: 1.0\n";
$headers .= "Content-type: text/html; charset= iso-8859-1\n";
$subject = "Error Page Accessed: " . Config::$_COMPANY_NAME;
$msg = "Error Page Accessed" . "<br /><br />";
$msg .= "Company Name: " . Config::$_COMPANY_NAME . "<br />";
$msg .= "Company Site: " . Config::$_UNSECURE_WEBSITE . "<br /><br />";
$msg .= "Previous Page the User Was On" . "<br />";
$msg .= $this->_previous_page . "<br /><br />";
$msg .= "Current Page Triggering Error" . "<br />";
$msg .= "<a href='" . $this->_current_page . "'>" . $this->_current_page . "</a>" . "<br /><br />";
$msg .= "The Error Displayed" . "<br /><br />";
$msg .= $error_message;
$to = Config::$_WEBMASTER_EMAIL;
@mail($to, $subject, $msg, $headers);
}
/**
* Stores the error in the error table
* I opted out from storing the message itself since I'll be able to replicate it when accessing the page triggering it
* Also, the error message itself is sent via email.
*
* @return void
*/
private function storeErrorMessage() {
$sql = "INSERT INTO error (previous_page, current_page) VALUES (?, ?)
ON DUPLICATE KEY UPDATE times_accessed = times_accessed + 1";
if (!$stmt = $this->_mysqli->prepare($sql)) {
$error = "Method: " . __FUNCTION__ . "() | File: " . __FILE__ . " | Line:" . __LINE__ . " | SQL Error: " . $this->_mysqli->error;
$this->sendErrorToWebmaster($error);
die ();
}
if (!$stmt->bind_param("ss", $this->_previous_page, $this->_current_page)) {
$error = "Method: " . __FUNCTION__ . "() | File: " . __FILE__ . " | Line:" . __LINE__ . " | SQL Error: " . $stmt->error;
$this->sendErrorToWebmaster($error);
die ();
}
if (!$stmt->execute()) {
$error = "Method: " . __FUNCTION__ . "() | File: " . __FILE__ . " | Line:" . __LINE__ . " | SQL Error: " . $stmt->error;
$this->sendErrorToWebmaster($error);
die ();
}
if (!$stmt->store_result()) {
$error = "Method: " . __FUNCTION__ . "() | File: " . __FILE__ . " | Line:" . __LINE__ . " | SQL Error: " . $stmt->error;
$this->sendErrorToWebmaster($error);
die ();
}
$stmt->close();
}
/**
* Displays to the user that there was an error and that the webmaster has already been notified
*
* @return void
*/
public function displayNotifiedMsg() {
?>
<div class='alert alert-danger' role='alert'>
The webmaster has been automatically notified. If you would like to add additional notes, please send us an email to:
<?php echo Config::$_WEBMASTER_EMAIL; ?>
</div>
<?php
}
/**
* Closes the database connection
*/
public function __destruct() {
$this->_mysqli->close();
}
}

Comments

Popular posts from this blog

Beginner Java Exercise: Sentinel Values and Do-While Loops

In my previous post on while loops, we used a loop-continuation-condition to test the arguments. In this example, we'll loop at a sentinel-controlled loop. The sentinel value is a special input value that tests the condition within the while loop. To jump right to it, we'll test if an int variable is not equal to 0. The data != 0 within the while (data != 0) { ... } is the sentinel-controlled-condition. In the following example, we'll keep adding an integer to itself until the user enters 0. Once the user enters 0, the loop will break and the user will be displayed with the sum of all of the integers that he/she has entered. As you can see from the code above, the code is somewhat redundant. It asks the user to enter an integer twice: Once before the loop begins, and an x amount of times within the loop (until the user enters 0). A better approach would be through a do-while loop. In a do-while loop, you "do" something "while" the condition...

Creating your own ArrayList in Java

Wanted to show that certain data structures in Java can be created by you. In this example, we'll go ahead and create an ArrayList data structure that has some of the methods that the built in ArrayList class has. We'll create 2 constructors: The default constructor that creates an ArrayList with a default size of 10. Constructor that allows an initial size to be passed to the array. We'll also create a number of methods: void add(Object x);  A method that allows you to place an Object at the end of the ArrayList. void add(int index, Object x);  A method that allows you to place a value at a given location. Object get(int index):  Allows you to retrieve a value of the arrayList array from a given location. int size();  Allows you to get the number of elements currently in the Arraylist. boolean isEmpty();  Tests to see if the Arraylist is empty. boolean isIn(Object x);  A method that sees if a particular object exist in the arrayList. int ...

Laravel 6.x with React and react-router

This will get you started on getting your first React/Laravel application deployed to your server. We'll cover everything from installation to deployment. Start by reading the installation instructions on  https://laravel.com/docs/6.x#installing-laravel . We'll cover those details below. Setting Up Laravel Check that you have the latest version of PHP installed on your computer.  It must be >= 7.2.0. Open terminal to get the Laravel installation tool. Type in composer global require laravel/installer Type in laravel to verify installation. Navigate to a directory on your computer where you want to install your project on your terminal. Run the following command: laravel new project_name (replace project_name with your project name). Once complete, cd into your new project. Type the following command: php artisan serve. You'll get a message like the following if it's running successfully: Laravel development server started: http://127.0.0.1:8000 ...