View Full Version : error handling

03-13-2007, 02:05 AM
how would you handle an error by redirecting to a certain page if an error occurs?

03-13-2007, 02:11 AM
If the error is something you have defined:

if ($error == something)

You can handle it with simple if/else statements.
If its a php defined error:

// code you want to test
catch(Exception $e)
// what to do when an exception is caught

Now, I'll tell you right now, I've never, not once, used a try / catch with a php script. Unfortunatly, when I had tested it out (common to Java and other languages), it does not catch fatal exceptions/errors, or compilation errors. For that reason, I've completely avoided them.
Oh, a link for you too :)

03-13-2007, 02:24 AM
thank you i didnt know there was try catch in php

03-13-2007, 02:32 AM
Neither did I until a few weeks ago, lol

03-13-2007, 02:35 AM
u could put an if statment at the top then use headers.

this would have to go at the very top of the page before the doctype or html tag eg:

if($error == "An Error Here!") {

Then on error.php you could have:

<title>There Was An Error!</title>

<h1>Sorry, There was an error!</h1>
There was an error on the last page!<br />
Please report this error by filling in the form below<br /><br />
<form action="mail.php">
Name: <input type="text" id="name" /><br />
Report:<br />
<textarea cols="10" rows="5" id="errorreport"><?php echo $error; ?></textarea><br />
Please do not edit the message above. It is an error report and will be sent to out site management team.
<br />
<input type="submit" />

Then in mail.php you would just use $_POST['blah']; to get the info and sent it using:


If you get stuck try: http://php.about.com/

03-13-2007, 06:20 AM
if($error == "An Error Here!") {

Just make sure you don't output anything before the header() function or it will die with an error about headers already being sent.

Also check out error_reporting() (http://www.php.net/error_reporting) & trigger_error() (http://www.php.net/manual/en/function.trigger-error.php).