PHP Error Handler can send you comprehensive error reports via email as well as output to the screen if you so choose.
- Get notified via email of php errors occurring on your website
- Option to use mysqli or pdo database connection
- Send error reports via email, display to the screen, or both
- Can log all errors in a database, with customizable retention period
- Flood control makes sure you don't get blasted with multiple emails with the same error within a configurable time period
- Send to one or more email recipients, including cc and bcc options
- Only send reports for the error types you choose (errors, warnings, notices, deprecations)
Written by: Kevin Roth - https://kevinroth.com
Released under the MIT license - http://opensource.org/licenses/MIT
- PHP >= 5.4
- MySQL or other PDO compatible database for logging & flood control features
Run the following command in your command line shell in your php project
$ composer require rothkj1022/php-error-handler
Done.
You may also edit composer.json manually then perform composer update
:
"require": {
"rothkj1022/php-error-handler": "^2.0.0"
}
require('vendor/autoload.php');
use rothkj1022\PHPErrorHandler;
$errorHandler = new PHPErrorHandler\PHPErrorHandler();
require('src/class.errorhandler.php');
use rothkj1022\PHPErrorHandler;
$errorHandler = new PHPErrorHandler\PHPErrorHandler();
After including the class file via autoload.php or directly, instantiate the object with a json array like this:
$errorHandler = new PHPErrorHandler\PHPErrorHandler([
'displayErrors' => false,
'emailErrors' => true,
'logErrors' => true, //requires database
'purgeLogTimeout' => '1 DAY', //use mysql date_add interval syntax or set to false
//'cacheFolder' => '/tmp/', //Folder for caching lookups, such as ip addresses. Must end with slash.
'floodControl' => '15 MINUTE', //use mysql date_add interval syntax or set to false
'database' => [
/*'driver' => 'mysql', //pdo or mysql
'hostname' => DB_HOSTNAME,
'username' => DB_USERNAME,
'password' => DB_PASSWORD,
'database' => DB_DATABASE,
'port' => DB_PORT,
'charset' => DB_CHARSET,*/
'driver' => 'pdo', //pdo or mysql
'dsn' => 'mysql:host='.DB_HOSTNAME.';dbname='.DB_DATABASE.';port='.DB_PORT.';charset=UTF8',
'username' => DB_USERNAME,
'password' => DB_PASSWORD
],
'email' => [
'recipients' => [
'to' => [
[
'address' => '[email protected]',
'name' => 'Test Guy'
]
],
'from' => [
'address' => '[email protected]',
'name' => 'No Reply'
]
]
]);
Display error details to screen (not recommended for production)
default: false
options: boolean (true / false)
Email error reports to configured recipient(s)
default: false
options: boolean (true / false)
Log errors to configured database
default: false
options: boolean (true / false)
Folder for caching lookups, such as ip addresses
default: false
options: string (full path to the cache folder)
If configured, purges logs in database older than the given interval.
Use mysql date_add interval syntax or set to false
default: '1 DAY'
options: string (date_add interval syntax), false
If database is configured, does not send repeat errors via email within set interval
Use mysql date_add interval syntax or set to false
default: '15 MINUTE'
options: string (date_add interval syntax), false
Json array of database configuration options
default: []
options: array
[
'driver' => 'mysql',
'hostname' => 'localhost',
'username' => 'mysqluser',
'password' => 'mysqlpass',
'database' => 'mydatabase',
'port' => 3306,
'charset' => 'utf8',
'table' => 'error_reports'
]
[
'driver' => 'pdo',
'dsn' => 'mysql:host=localhost;dbname= mydatabase;port=3306;charset=UTF8',
'username' => 'mysqluser',
'password' => 'mysqlpass'
]
Driver to be used for the database connection
default: 'mysql'
options: 'mysql', 'pdo'
DSN connection string for PDO connections
default: ''
options: string (pdo dsn connection string)
Host name of the database server
default: 'localhost'
options: string (server host name)
Database user name
default: ''
options: string (db username)
Database password
default: ''
options: string, (db password)
Database name
default: ''
options: string (db name)
Database port
default: 3306
options: integer (port number)
Database character set
default: 'utf8'
options: string (db charset)
Database table name for logging error reports
default: 'error_reports'
options: string (db table name)
Array of to, cc, or bcc types
Array of contacts (name, address)
Email address of the contact
default: null
options: string (valid email address)
Name of the contact
default: null
options: string
Email address of the contact
default: null
options: string (valid email address)
Name of the contact
default: null
options: string
Email address of the contact
default: null
options: string (valid email address)
Name of the contact
default: null
options: string
default: 'PHP Error Report from ' . $_SERVER['SERVER_NAME']
options: string
Json array of email configuration options. See PHPMailer documentation for detailed default options.
default: 'utf-8'
options: string
default: false
options: boolean (true, false)
default: 'localhost'
options: string
default: 25
options: integer
Get debug info for SMTP sending. See SMTP Debugging for more info.
See also PHPMailer SMTPDebug property documentation
default: 0 (no output)
options: integer
Whether to enable TLS encryption automatically if a server supports it, even if `SMTPSecure` is not set to 'tls'.
See also PHPMailer SMTPAutoTLS property documentation
default: true
options: boolean (true, false)
Enable SMTP authorization
default: false
options: boolean (true, false)
SMTP account username / email address
default: ''
options: string
SMTP Password
default: ''
options: string
Type of encryption used for SMTP sending
default: 'tls'
options: string ('ssl', 'tls')
See PHPMailer SMTPOptions property documentation
default: []
options: array
// Disable verification for self-signed ssl certificates
[
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
Array of PHP error types that you want to be handled
default: [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR]
options: array
Array of PHP warning types that you want to be handled
default: [E_WARNING, E_CORE_WARNING, E_COMPILE_WARNING, E_USER_WARNING, E_RECOVERABLE_ERROR]
options: array
Array of PHP warning types that you want to be handled
default: [E_NOTICE, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED]
options: array
Whether or not to process errors
default: true
options: boolean (true, false)
Whether or not to process warnings
default: true
options: boolean (true, false)
Whether or not to process notices
default: false
options: boolean (true, false)
Your API token for ipinfo.io if you have one
default: '';
options: string
Plugin method | Description |
---|---|
mysqlError($errorMsg, $sql, $errfile = null, $errline = 0, $die = false) | Send a MySQL-specific error report, including the query. $errorMsg = the error message to send, usually $mysqli->error. $sql = the query. $errfile = the file in which the error occurred, called by using __FILE__. $errline = the line of the file on which the error occurred, called by using __LINE__. $die = whether or not to stop processing the script after sending the error. See example below. |
sendError($errorMsg, $msgDetails = '', $errfile = null, $errline = 0, $die = false) | Send a custom error report. $errorMsg = the error message to send. $msgDetails = further details regarding your custom error. $errfile = the file in which the error occurred, called by using __FILE__. $errline = the line of the file on which the error occurred, called by using __LINE__. $die = whether or not to stop processing the script after sending the error. See example below. |
$mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PORT);
$sql = "select blah from products limit 3";
$query = $mysqli->query($sql) or $errorHandler->mysqlError($mysqli->error, $sql, __FILE__, __LINE__);
$errorHandler->sendError('$myVar is not defined.', 'You should really define that variable.', __FILE__, __LINE__);
- Added config var for ipinfo.io API token
- Fixed flood control
- Updated ip address lookups to fetch with Guzzle for better reliability.
- Added cacheFolder option for caching remote lookups
- Added config var for allowing change of PHPMailer SMTPAutoTLS setting
- Added config vars for allowing change of reply-to address
- Added config vars to disable processing errors, warnings, and notices
- Fixes for composer integration
- Code overhaul with composer integration
- Added changelog, readme documentation
- Enhancement: added PDO database option