...

View Full Version : error handling



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

Fou-Lu
03-13-2007, 01: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:


try
{
// 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 :)
http://ca3.php.net/manual/en/language.exceptions.php

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

Fou-Lu
03-13-2007, 01:32 AM
>.<
Neither did I until a few weeks ago, lol

the-dream
03-13-2007, 01: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:




<?php
if($error == "An Error Here!") {
header('error.php');
}



Then on error.php you could have:




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

<body>
<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" />
</form>




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


mail();

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

Inigoesdr
03-13-2007, 05:20 AM
<?php
if($error == "An Error Here!") {
header('error.php');
}

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum