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):
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:
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.
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> © 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.