SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Custom Error Handling

    I recently bought "PHP and MySQL Web Development" by Luke Welling and Laura Thomson. A part of it describes how to create your own custom error handler so that your visitors are presented with something a bit more pleasant then a general PHP error. I have done this and the code extract below shows a customer error handling function. Below the function is the function which lets PHP know what to run when an error is found (in this case the "error_handler" function at the top).

    PHP Code:
    <?php

    function error_handler ($errno$errstr) {

    echo(
    "<html>
          <head>
          <title>Sorry, there was an error</title>
          <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
          <link rel='stylesheet' href='style.CSS' type='text/css'>
          </head>

          <body bgcolor='#E3E3E3' text='#000000'>

          <p>There was an error while generating the page. This is <i>probably</i> not your 
          fault,<br>and just to make sure, try clicking refresh. The webmaster has been emailed<br> 
          about this error.</p>
          
          <p><b>Error:<br>
          <textarea name='error' cols='65' rows='5'>Line "
    .__LINE__." of ".__FILE__.". 

    Error was: 
    $errstr</textarea>
          </b></p>
          
          </body>
          </html>"
    );
          
          
    //mail the webmaster
          // mail( $settings["webmasteremail"], "epiGamer Error", "There was an error on the epiGamer Site. Error information below.
          //
          //Line ".__LINE__." of ".__FILE__.". 
          //
          //Error was: $errstr");
          
          //end program
          
    exit;
          
    }

    //activate the custom error handling
    set_error_handler("error_handler");


    ?>
    The problem is that the set_error_handler("error_handler"); bit seems to result in an error. Without it, the page is shown and generated without a problem, with it, the error_handler function is run and the following error is shown: Line 19 of c:\Program Files\Apache Group\Apache\htdocs\epigamer\inc\fns_errors.php.

    Error was: Use of undefined constant content - assumed 'content'
    .

    This confuses me somewhat since I haven't defined any constants anywhere, and the only ones used are LINE and FILE which are both passed from PHP when an error is created anyway.

    Does anyone have any ideas where this CONTENT variable is coming from? My database is called "content" but I don't see how that could be related, as this script preceeds the connection to the database.

    Thanks as always.
    Regards, Ant.

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I forgot to point out that this is what is on line 19:

    <textarea name='error' cols='65' rows='5'>Line ".__LINE__." of ".__FILE__.".
    Regards, Ant.

  3. #3
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you probably have something like $array[content] in your code when it should be $array['content']. you always need to quote associative array keys.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR


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
  •