Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: error handling

  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts

    error handling

    how would you handle an error by redirecting to a certain page if an error occurs?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    If the error is something you have defined:
    PHP Code:
    if ($error == something)
    {
      ...

    You can handle it with simple if/else statements.
    If its a php defined error:
    PHP Code:
    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
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you i didnt know there was try catch in php

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    >.<
    Neither did I until a few weeks ago, lol
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    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 Code:

    <?php
    if($error == "An Error Here!") {
    header('error.php');
    }
    Then on error.php you could have:

    PHP Code:

    <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:

    PHP Code:
    mail(); 
    If you get stuck try: http://php.about.com/

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by the-dream View Post
    PHP Code:
    <?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() & trigger_error().


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •