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);
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);
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
Post a Comment