Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you prevent error message repeat? 1

Status
Not open for further replies.

thenewa2x

Programmer
Dec 10, 2002
349
US
I created my own function to replace the way PHP handles certain errors by using set_error_handler(). For some reason when I want to prevent the warning from being displayed by using the @, the warning is displayed anyway.

Here's my function (php_error):
Code:
    /**
    * PHP Error Handler
    *
    * //
    *
    * @access public
    * @return void
    */
    function php_error ( $number,
                         $string,
                         $file,
                         $line )
    {
        // Create path.
        $path = "/" . preg_quote( ROOT_DIR,
                                  "/" ) . "/";

        // Log before cleaning.
        error_log( "$string at line $line in $file.", 0 );

        // Clean message.
        $string = preg_replace( $path,
                                "./",
                                $string );

        // Clean file.
        $file = preg_replace( $path,
                              "./",
                              $file );

        // Create translator.
        $translate = array(

            E_ERROR             => "E_ERROR",
            E_WARNING           => "E_WARNING",
            E_PARSE             => "E_PARSE",
            E_NOTICE            => "E_NOTICE",
            E_CORE_ERROR        => "E_CORE_ERROR",
            E_CORE_WARNING      => "E_CORE_WARNING",
            E_COMPILE_ERROR     => "E_COMPILE_ERROR",
            E_COMPILE_WARNING   => "E_COMPILE_WARNING",
            E_USER_ERROR        => "E_USER_ERROR",
            E_USER_WARNING      => "E_USER_WARNING",
            E_USER_NOTICE       => "E_USER_NOTICE",
            E_STRICT            => "E_STRICT",

        );



        // Switch on errors.
        switch ( $number )
        {
            // Error.
            case E_USER_ERROR:
            case E_ERROR:
            case E_CORE_ERROR:
            case E_PARSE:
            case E_COMPILE_ERROR:
            {
                // Print error.
                ?>
        <html>
            <head>
                <title>Pheera CMS</title>
                <style type="text/css">
                    <!--
                    body,td,th {
                            font-family: Arial, Helvetica, sans-serif;
                            font-size: 11px;
                            color: #000000;
                    }
                    body {
                            background-color: #FFFFFF;
                            margin-left: 10px;
                            margin-top: 10px;
                            margin-right: 10px;
                            margin-bottom: 10px;
                    }
                    .table {
                            width: 350px;
                            border: 1px solid #CCCCCC;
                    }
                    .cellt {
                            padding-top: 2px;
                            padding-right: 4px;
                            padding-bottom: 2px;
                            padding-left: 4px;
                            border-bottom-width: 1px;
                            border-bottom-style: solid;
                            border-bottom-color: #CCCCCC;
                            background-color: #F7F7F7;
                            font-weight: bold;
                    }
                    .cellm {
                            padding-top: 7px;
                            padding-right: 4px;
                            padding-bottom: 7px;
                            padding-left: 4px;
                    }
                    .cellb {
                            padding-top: 2px;
                            padding-right: 4px;
                            padding-bottom: 2px;
                            padding-left: 4px;
                            border-top-width: 1px;
                            border-top-style: solid;
                            border-top-color: #CCCCCC;
                            background-color: #F7F7F7;
                    }
                    a:link {
                            color: #CC0000;
                            text-decoration: none;
                    }
                    a:visited {
                            color: #CC0000;
                            text-decoration: none;
                    }
                    a:hover {
                            color: #666666;
                            text-decoration: none;
                    }
                    a:active {
                            color: #CC0000;
                            text-decoration: none;
                    }
                    -->
                </style>
            </head>
            <body>
                <table border="0" align="center" cellpadding="0" cellspacing="0"
                                                                  class="table">
                  <tr>
                    <td class="cellt">
                        Pheera CMS </td>
                  </tr>
                  <tr>
                    <td class="cellm">
                      <table width="100%"  border="0" cellpadding="0"
                                                  cellspacing="0" class="table">
                        <tr>
                          <td width="20%" class="cellt" align="center">Code</td>
                          <td width="60%" class="cellt">File</td>
                          <td width="20%" class="cellt">Line</td>
                        </tr>
                        <tr>
                          <td class="cellm" align="center">
                          <?= $translate[ $number ] ?></td>
                          <td class="cellm"><?= $file ?></td>
                          <td class="cellm"><?= $line ?></td>
                        </tr>
                      </table>
                      <br>
                    <br>
                    <?= $string ?>
                    <br>
                    <br>
                    </td>
                  </tr>
                  <tr>
                    <td class="cellb">Powered by
                    <a href="[URL unfurl="true"]http://www.pheera.com/">Pheera[/URL] CMS</a> &copy; 2005
                    <a href="[URL unfurl="true"]http://www.pheera.com/">Kevin[/URL] Herrera</a>. </td>
                  </tr>
                </table>
            </body>
        </html>
                <?

                // Exit.
                exit( );
            }

            // Warning?
            case E_USER_WARNING:
            case E_WARNING:
            case E_CORE_WARNING:
            case E_COMPILE_WARNING:
            {
                // Print warning.
                ?>
        <table width="100%"  border="0" cellspacing="0" cellpadding="4"
        style="border: 1px solid #CCCCCC;
        font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
          <tr>
            <td width="98" rowspan="2" align="center"><b>Warning</b></td>
            <td width="20" align="center" style="background-color: #F7F7F7;
            border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;
            border-bottom: 1px solid #CCCCCC;">Code</td>
            <td width="259"
            style="border-bottom: 1px solid #CCCCCC;">
            <?= $translate[ $number ] ?></td>
            <td width="16" align="center" style="background-color: #F7F7F7;
            border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;
            border-bottom: 1px solid #CCCCCC;">File</td>
            <td width="278"
            style="border-bottom: 1px solid #CCCCCC;"><?= $file ?></td>
            <td width="20" align="center" style="background-color: #F7F7F7;
            border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;
            border-bottom: 1px solid #CCCCCC;">Line</td>
            <td width="283"
            style="border-bottom: 1px solid #CCCCCC;"><?= $line ?></td>
          </tr>
          <tr>
            <td colspan="6" style="border-left: 1px solid #CCCCCC;
            padding-left: 6; padding-right: 6;"><?= $string ?></td>
          </tr>
        </table>
        <br>
                <?

                break;
            }

            // Notice?
            case E_USER_NOTICE:
            case E_NOTICE:
            case E_STRICT:
            {
                // Don't print notices?
                if ( defined( 'SHOW_NOTICES' ) )
                    return( null );

                // Print notice.
                ?>
        <table width="100%"  border="0" cellspacing="0" cellpadding="4"
        style="border: 1px solid #CCCCCC;
        font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
          <tr>
            <td width="98" rowspan="2" align="center"><b>Notice</b></td>
            <td width="20" align="center" style="background-color: #F7F7F7;
            border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;
            border-bottom: 1px solid #CCCCCC;">Code</td>
            <td width="259"
            style="border-bottom: 1px solid #CCCCCC;"><?= $number ?></td>
            <td width="16" align="center" style="background-color: #F7F7F7;
            border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;
            border-bottom: 1px solid #CCCCCC;">File</td>
            <td width="278"
            style="border-bottom: 1px solid #CCCCCC;"><?= $file ?></td>
            <td width="20" align="center" style="background-color: #F7F7F7;
            border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;
            border-bottom: 1px solid #CCCCCC;">Line</td>
            <td width="283"
            style="border-bottom: 1px solid #CCCCCC;"><?= $line ?></td>
          </tr>
          <tr>
            <td colspan="6" style="border-left: 1px solid #CCCCCC;
            padding-left: 6; padding-right: 6;"><?= $string ?></td>
          </tr>
        </table>
        <br>
                <?

                break;
            }
        }
    }

I use [tt]user_error( "The message.", E_USER_WARNING );[/tt] to report my errors but I also use the @ to do other things while preventing the warning from being displayed.

Example code:
Code:
    // Function.
    function file_check ( $file = "" )
    {
        // File does not exist?
        if ( !file_exists( $file ) )
        {
            // Report.
            user_error( "The file, $file, does not exist.",
                        E_USER_WARNING );
        }
        
        return( true );
    }
    
    // File exist?
    if ( !@file_exists( "path/to/file.blah" ) )
    {
        // Report different message.
        user_error( "My file, file.blah, doesn't exist.  Maybe you should " .
                    "create it?",
                    E_USER_ERROR );
    }

The error message displayed should be "My file, file.blah,..." not "The file, $file,...". Instead, both messages are shown.

How do I fix this?

---------------------------------------
TINSTAAFL, which is why I contribute.
 
what if you tried this:
Code:
$result=file_exists("path/to/file.blah") or user_error(...);

"Together we can make 'computer illiterate' a dirty word."
 
It was an example. My other functions and methods are much more complex.

---------------------------------------
TINSTAAFL, which is why I contribute.
 
perhaps it's a server-side thing. I know that you can turn off error display in the PHP module. maybe you can turn off the @ flag?

can I see a phpinfo()?

"Together we can make 'computer illiterate' a dirty word."
 
What do you need to see in phpinfo()?

---------------------------------------
TINSTAAFL, which is why I contribute.
 
was going to look at your configuration of the modules and error processing to see if there's something that is missing or turned off.

"Together we can make 'computer illiterate' a dirty word."
 
since I feel that phpinfo( ) contains some sensitive information I will not show it. just tell me what you need to know.

---------------------------------------
TINSTAAFL, which is why I contribute.
 
The error suppressor '@', temporarily sets error_reporting to zero. PHP's standard error handler checks this value and if it set to zero then it does not display the error. By using your own custom error handler PHP completely bypasses the standard handler, hence '@' has no effect.

To achieve the result you would like, you need to change your error handler to also check the error_reporting value. Example code taken from PHP Manual:
Code:
<?php
function my_error_handler(.......)
{
  // check error reporting value if 0 '@' was used
  if(error_reporting() == 0) 
  {
   // do nothing
  }
  else
  {
   // do something
  }
}
?>

As a side note:
PHP Manual said:
The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.
 
Ahhhh!!!! Thank you! *bows*

As for the side note, I already knew that. The only reason I included the other error codes is because I want to be able to support them in case the PHP devs decide to allow the user to handle them.

---------------------------------------
TINSTAAFL, which is why I contribute.
 
sorry I was no help

"Together we can make 'computer illiterate' a dirty word."
 
it's ok, atleast you tried.

---------------------------------------
TINSTAAFL, which is why I contribute.
 
sysadmin42
You may not have had the answer, but you were on the right track. error_reporting and display_errors are a settings within the php.ini file, you just needed to take your thought one step further and determine exactly what '@' changes for PHP not to report an error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top