SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: Hiding error messages - custom
-
Jun 28, 2003, 16:56 #1
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.
-
Jun 28, 2003, 17:34 #2
- 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_ERROR, E_USER_WARNING, E_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");
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;
}
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%"> </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%"> </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>
Enjoy.
-
Jun 28, 2003, 18:24 #3
bear in mind that custom error handlers cannot catch parse errors...
-
Jun 28, 2003, 20:09 #4
Wow. Thanks for the code, that's a lot of stuff to play with right now, I'll check it out.
-
Jun 28, 2003, 20:18 #5
- 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