SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Threaded View

  1. #1
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sharing Code - Handling website errors

    I just dealt with an error that was discussed in this forum post: Login-form-problems-making-me-go-bald!!! and it got me to thinking about error handling. So I've taken time this morning to write up some code to handle errors a little more gracefully, and with feedback via email to a specified recipient.

    I'm open to all critiques of this method and my code to help improve it, but thought it may be beneficial to some and I'd like to try to give back to at least a few people here with time I've put out to maybe make someone's project go a little easier.

    I am posting this under a Creative Commons Attribution-ShareAlike license click the link to read the details. If you use the code, edited or unedited, I'd love to hear about it: <snip>e-mail address removed</snip>

    This code is stored in an include file that you would call on every page of your website in the first line:
    Mine is called: init.inc.php
    PHP Code:
    <?php
    // This header MUST be included in all variations/uses
    // Script written by: Greg Hicks
    // Date written: 9/5/2012
    // Author email: greg@tekamba.com
    // Licensed using CC Attribution-ShareAlike licensing
    // Details can be found here: http://creativecommons.org/licenses/by-sa/3.0/

    // If no session exists start one
    if (session_id() == '')
    {
      
    session_start();
    }

    // #####################################
    // # File variables - modify as needed #
    // #####################################

    // Do you want error reporting on or off?
    // 0:disable - 1:enable
    $seterrorrpt "1";

    // Database connection - variables
    $dbserv "localhost"// modify if on separate server
    $dbtouse "SET DATABASE";
    $dbuser "SET DB USER";
    $dbpass "SET DB PASSWORD";
    $dbtable "SET DB TABLE";
    $dblink ""// Variable for script db access

    // Set a friendly name for this website
    // Keep it fairly brief
    $sitename "FRIENDLY NAME";
    $domainname "YOURDOMAIN.com";

    // Who to notify in case of db error
    $contactname "SET YOUR NAME";
    $contactbiz "SET BUSINESS NAME"// OPTIONAL
    $contactphone "SET PHONE";
    $contactemail "SET EMAIL";

    // Base directory of website
    // Comment/uncomment whichever method is desired
    //$rootdir = "http://www.YOURDOMAIN.com";
    $rootdir $_SERVER['SERVER_NAME'];

    // #############################################
    // # BEGIN CODE - USE CAUTION EDITING FILE BELOW THIS LINE!!! #
    // ############################################

    // Error reporting - comment out for deployment
    if ($seterrorrpt == "1")
    {
      
    ini_set('display_errors'1);
      
    ini_set('log_errors'1);
      
    ini_set('error_log'dirname(__FILE__) . '/error_log.txt');
      
    error_reporting(E_ALL);
    }

    // Make connection to database
    $dblink mysqli_connect($dbserv$dbuser$dbpass);
    if (!
    $dblink)
    {
      
    $errornotice 'Unable to connect to the database server.';
      include (
    'includes/report-error.php');
      exit();
    }

    if (!
    mysqli_set_charset($dblink'utf8'))
    {
      
    $errornotice .= 'Unable to set database connection encoding.';
      include 
    $rootdir '/includes/report-error.php';
      exit();
    }

    if (!
    mysqli_select_db($dblink$dbtouse))
    {
      
    $errornotice .= 'Unable to locate the database.';
      include 
    $rootdir '/includes/report-error.php';
      exit();
    }

    ?>
    Here is the report-error.php file used in previous script stored in an "includes" directory of main site
    PHP Code:
    <?php
    // This header MUST be included in all variations/uses
    // Script written by: Greg Hicks
    // Date written: 9/5/2012
    // Author email: greg@tekamba.com
    // Licensed using CC Attribution-ShareAlike licensing
    // Details can be found here: http://creativecommons.org/licenses/by-sa/3.0/

    // Hash the password for basic security
    $dbpass md5($dbpass);
    // Set default timezone for admin (adjust from server timezone)
    // Use ONLY if your server is in a different timezone and your not sure what to edit in .htaccess
    // I ended up using the .htaccess method myself
    // For list of US zones: http://www.php.net/manual/en/timezones.america.php
    //date_default_timezone_set('America/Phoenix');
    // Capture the time error occurred
    $errortime date('M d, Y h:i A');
    // Set subject for emailed report
    $errorsubject $domainname " ERROR: diagnostic info";

    // Get the error thrown
    $phpgeterror error_get_last();
    $phperror "File " $phpgeterror['file'] . " caused error " $phpgeterror['message'] . " at line " $phpgeterror['line'];

    // Which script file errored
    $badscript $_SERVER['SCRIPT_FILENAME'];

    // Gather some basic visitor info
    $visitorip $_SERVER['REMOTE_ADDR'];
    $visitorbrowser $_SERVER['HTTP_USER_AGENT'];

    // Error message to display to user
    // Used when error reporting is disabled
    $errormsg_basic = <<<EOF
    The website at $domainname has errored.<br />
    Please notify the Admin that you received this error.<br />

    <b>Site that error occurred on:</b> 
    $badscript<br />
    <b>Error:</b> 
    $phperror<br />
    <b>Handled Error:</b> 
    $errornotice<br />
    <br />
      ---------------------------------<br />
      <b>Admin Name:</b> 
    $contactname<br />
      <b>Business Name:</b> 
    $contactbiz<br />
      <b>Admin Email:</b> 
    $contactemail<br />
      <b>Admin Phone:</b> 
    $contactphone<br />
      ---------------------------------<br />
      <b>Reported Base Directory:</b> 
    $rootdir<br />
    EOF;

    // Error message to display to user
    // Used when error reporting is enabled
    // and for the email sent to admin
    $errormsg_verbose = <<<EOF
    The website at $domainname has errored. Below is the diagnostic information.
    Please notify the Admin that you received this error.

    Site that error occurred on: 
    $badscript
    Error: 
    $phperror
    Handled Error: 
    $errornotice

    User Info:
      IP of Visitor: 
    $visitorip
      Browser: 
    $visitorbrowser
      Time of error: 
    $errortime

    Variables:
      Error Reporting: 
    $seterrorrpt
      Database Server: 
    $dbserv
      Database: 
    $dbtouse
      Database User: 
    $dbuser
      Database Password: 
    $dbpass
      Database Table: 
    $dbtable
      ---------------------------------
      Admin Name: 
    $contactname
      Business Name: 
    $contactbiz
      Admin Email: 
    $contactemail
      Admin Phone: 
    $contactphone
      ---------------------------------
      Reported Base Directory: 
    $rootdir

    NOTE: This information provided for troubleshooting purposes only!
    Please reference this info when contacting admin for support.
    EOF;

    // Send an error report to the admin
    if (mail($contactemail$errorsubject$errormsg_verbose))
    {
      
    $emailsuccess "The administrator has been notified,";
    } else {
      
    $emailsuccess "The administrator has NOT been notified,";
    }
    ?>

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title><?php echo $sitename?> Error Reported</title>
    </head>
    <style>
      body {
        background-color: #c0c0c0;
      }

      .mainbody {
        background-color: #f0f0f0;
        width: 960px;
        border: 2px solid #ff0000;
        color: #000000;
        font-size: 14pt;
        padding: 3px;
        margin: auto;
      }
    </style>
    <body>
      <div class="mainbody">
        <h1><?php echo $sitename?> Reported an Error</h1>
        <h3><?php echo $emailsuccess?> you are welcome to send them this info yourself as well.</h3>
        <?php
          
    echo $errormsg_basic;
        
    ?>
        <h4>
          Return to Home Page of Website: <a href="http://<?php echo $rootdir?>"><?php echo $sitename?></a>
          <br />
          <a href='javascript:history.back()>RETURN TO PREVIOUS PAGE</a>
        </h4>
      </div>
    </body>
    </html>
    And finally, here is a junky little test script to ensure things are working, just set one of the variables in the init.inc.php file to make it error.
    PHP Code:
    <?php
      
    include('includes/inc_initialize.inc.php');
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>No Error</title>
      <link rel="stylesheet" href="style.css">
      <script src="script.js"></script>
    </head>
    <body>
    If you see this page no error was thrown.
    </body>
    </html>
    Last edited by molona; Sep 6, 2012 at 09:46. Reason: removing fake signature and personal details


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •