SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot grantus's Avatar
    Join Date
    Apr 2003
    Location
    TX
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hiding error messages - custom

    Hi,

    Right now I have my pages in PHP (no database) with 3 includes: header, nav, and footer. Once in a while I've had errors with PHP on my site (various reasons) and I got those messy, long error messages. I was browsing around here and saw something that said all I have to put in my include is add "@". So my include would be
    < include @ "header.php">

    That works fine, when there's an error it just doesn't show my header at all. My question is though, how can I set it so that instead of not showing anything at all, it shows a custom error message like CANNOT BE DISPLAYED. Or something like that.

    Any suggestions? Thanks.

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm... To help you out here is what I use and it works fine for a number of errors you might pick up:

    Place the following below in pages where you want error trapping/reporting:

    PHP Code:
    error_reporting(0);
        
        function 
    errorHandler($errno$errmsg$filename$linenum) {
            
    # create array to hold type of error
            
    $errorType = array (
                
    1       =>  "Error",
                
    2       =>  "Warning",
                
    4       =>  "Parsing Error",
                
    8       =>  "Notice",
                
    16      =>  "Core Error",
                
    32      =>  "Core Warning",
                
    64      =>  "Compile Error",
                
    128     =>  "Compile Warning",
                
    256     =>  "User Error",
                
    512     =>  "User Warning",
                
    1024    =>  "User Notice");
                
            
    $userError = array(E_USER_ERRORE_USER_WARNINGE_USER_NOTICE);
            
            if(
    in_array($errno$userError)) {    
                
    # found an error so alert user
                
    header('location:errors.php?msg=' $errmsg .'&name=' basename($filename) . '&num=' $linenum '&type=' $errorType[$errno]);
            }
        }
        
        
    # define error handler
        
    $error_handler set_error_handler("errorHandler"); 
    Next, to detect for an error, ie if a file INCLUDE isn't working, I use this:

    NOTE: In this very case, I am checking to see if an OOP class has been INCLUDEd or not; report error if it's not.

    PHP Code:
    #
        
    @ include_once(MAIN_DIRECTORY .'main.class.php');
        if(!
    class_exists('mySQLAccess')) {
            
    trigger_error('unable to install: main.class.php'E_USER_WARNING);
            return 
    false;
        } 
    So once re-directed to 'errors.php' the error message is passed via the URL (ie $_GET['...']) and displayed; errors.php file now follows below:

    PHP Code:
    <head>
        <title> Error Report </title>
        <link rel="stylesheet" type="text/css" href="templates/style.css" />
    </head>
    <body leftmargin="0" topmargin="12" marginwidth="0" marginheight="12">
        <table border="0" width="992" height="100%" align="center" valign="center" cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
        <td height="50%">&nbsp;</td></tr>
        <tr>
        <td align="center" valign="center" width="100%" height="64">
        
            <table style="border:solid 1px rgb(144, 184, 175);background-color:rgb(223, 239, 208);" border="0" width="75%" align="center" height="192" valign="center" cellspacing="0" cellpadding="0">
            <tbody>
            <tr>
            <td align="center" valign="top" width="100%" height="16">
            <b>ERROR REPORT - <?php echo(strtoupper($_GET['type'])); ?></b>
            </td></tr>
            <td align="center" valign="center" width="100%" height="100%">
            <p>An error has occured with this application.</p>
            <p><b><?php echo(strtoupper($_GET['msg'])); ?></b></p>
            <p>The error originated from file <b><?php echo($_GET['name']); ?></b> at line number <b><?php echo($_GET['num']); ?></b></p>
            <p>This error has now been logged - if it persists, please report this error to your administrator.</p>
            <p><button onClick='window.location="index.php?comm=admin";'>Go Back</button></p>
            </td></tr>
            </tbody></table>
        
        </td></tr>
        <tr>
        <td height="50%">&nbsp;</td></tr>
        </tbody></table>
        <?php 
        
    # log error to file
        
    $date date('Y-m-d H:i:s');
        
    $mess $date " [File: " $_GET['name'] . " - on line " $_GET['num'] . "; " $_GET['msg'] . " - Level: " $_GET['type'] . "]\r\n";
        
    # logging...
        
    if($fp = @ fopen('logs/error-log.txt'"a+")) {
            @ 
    fputs($fp$mess);
            @ 
    fclose($fp);
        }
        
    ?>
    </body></html>
    And there you have it. Another way of doing it (not shown) is to use output buffering, though I'll leave you with this stuff which'll keep you busy for just now.

    Enjoy.

  3. #3
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bear in mind that custom error handlers cannot catch parse errors...

  4. #4
    SitePoint Zealot grantus's Avatar
    Join Date
    Apr 2003
    Location
    TX
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow. Thanks for the code, that's a lot of stuff to play with right now, I'll check it out.

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oppss... Forgot to mention that But you're not likely to release scripts etc with parse errors in there anyway, right ? ... Hope not anyways


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
  •