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 9 of 9
  1. #1
    Senior Coder JamieR's Avatar
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    3,161
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Simple error manager

    PHP Code:
    <?php

    switch ($_GET['type']) 
    {
    case 
    404:
    $error 'Error 404 has occured';
    $error_message 'The Page You Requested cannot be Found.';
    break;
     
    case 
    403
    $error 'Error 403 has occured';
    $error_message 'You are not allowed to view this page or directory.';
    break;

    case 
    500:
    $error 'Error 500 has occured';
    $error_message 'Either your page cannot be found or there has been a script error. Please notify the Administrators.';
    break;

    case 
    401;
    $error 'Error 401 has occured';
    $error_message 'This server could not verify that you are authorized to access the specified URL. You either supplied the 

    wrong credentials (e.g. bad password), or your browser does not understand how to supply the credentials required.'
    ;
    break;

    case 
    400;
    $error 'Error 400 has occured';
    $error_message ' Error 400 - Your browser (or proxy) sent a request that this server could not understand.';
    break;
    }

    if (
    $error == false && $error_message == false) { /*If no error has occured (denoted by $error and $error_message not being 

    set*/

    echo '<p>' 'No error has occured' '</p>';
    } else {

    echo 
    '<h1>' $error .'</h1>';
        echo 
    '<p>' $error_message '</p>';

    }

    ?>
    an easy way to manage all your Apache/IIS error messages from one file. Just had it lying around so I thought I'd post it.

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by weazel
    PHP Code:
    <?php

    switch ($_GET['type']) 
    {
    case 
    404:
    $error 'Error 404 has occured';
    $error_message 'The Page You Requested cannot be Found.';
    break;
     
    case 
    403
    $error 'Error 403 has occured';
    $error_message 'You are not allowed to view this page or directory.';
    break;

    case 
    500:
    $error 'Error 500 has occured';
    $error_message 'Either your page cannot be found or there has been a script error. Please notify the Administrators.';
    break;

    case 
    401;
    $error 'Error 401 has occured';
    $error_message 'This server could not verify that you are authorized to access the specified URL. You either supplied the 

    wrong credentials (e.g. bad password), or your browser does not understand how to supply the credentials required.'
    ;
    break;

    case 
    400;
    $error 'Error 400 has occured';
    $error_message ' Error 400 - Your browser (or proxy) sent a request that this server could not understand.';
    break;
    }

    if (
    $error == false && $error_message == false) { /*If no error has occured (denoted by $error and $error_message not being 

    set*/

    echo '<p>' 'No error has occured' '</p>';
    } else {

    echo 
    '<h1>' $error .'</h1>';
        echo 
    '<p>' $error_message '</p>';

    }

    ?>
    an easy way to manage all your Apache/IIS error messages from one file. Just had it lying around so I thought I'd post it.
    But how would this work though for server generated errors, for example, going to a file that doesn't exist? Possibly mod_rewrite might do it... fool the server to going to 404.shtml when the file is really error.php?type=404

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or you could just set your Error 404 page to a page with a QueryString, mod_rewrite wouldn't be needed at all.

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by missing-score
    Or you could just set your Error 404 page to a page with a QueryString, mod_rewrite wouldn't be needed at all.
    Only depending on your host as well. If you don't have access to it then your touogh out of luck on that matter unless you have your host do it. I'm talking about file wise, not cpanel or anything.

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    but the point is, if you didn't have HTACCESS at all, then you wouldn't be able to define error pages in the normal way, and if you do, you can define the error pages, so I dont see the problem.

    weazel, out of interest, how do you use this on your website (if you use it at all).

  • #6
    Senior Coder JamieR's Avatar
    Join Date
    Oct 2004
    Location
    United Kingdom
    Posts
    3,161
    Thanks
    0
    Thanked 5 Times in 5 Posts
    ErrorDocument 404 /error.php?type=404
    etc....

    If you don't have access to .htaccess, you can't use it. The only reason I said IIS in my original post is because I believe you can define custom error pages from inetmgr (and some windows hosts support PHP)

  • #7
    Regular Coder zro@rtv's Avatar
    Join Date
    Feb 2005
    Location
    on the network
    Posts
    433
    Thanks
    0
    Thanked 1 Time in 1 Post

    neat! ...slightly off subject reply, here

    thats pretty cool...
    diving into php here, and as simple as that snippet is i think it just made a couple things a little more clear for me... thanks...

    ALSO....
    (prolly not the place to put this but, i stumbled across it searching for an answer so i thought id risk postin it...)
    On the subject of PHP and Error handling.... can't ya do this little JS error tailoring action server side in a pretty simple fashion so as to avoid the shortcommings of <noscript>?

    (sry to post basically a question in here, but i thought if that was touched on it might make this thread even more great!)
    ._-zro
    zro@redtv
    zro.redtv.org

    "If HTML and the Web made all the online documents look like one huge book, RDF, schema, and inference languages will make all the data in the world look like one huge database"
    -Tim Berners-Lee, Weaving the Web, 1999

  • #8
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Thats true. But what if your APACHE doesn't allow you to change error pages? For example I have a free PHP host that allows .htacces and mod_rewrite but adding 404 pages just dissapear because they have they're own. Using mod_rewrite you can still achieve the same result.

    Also if you don't have APACHE at all, or don't have access to it you can still use it if you have a dynamic include functions. If the file doesn't exist, you simply include the proper error instead.

    There are plenty of ways to use this without APACHE as even some scripts have they're own defined 404 pages which can be altered to this script as well.

    The only problem with this is, you don't really need the manager considering if you are doing things for the site it would usually only be a 404 error, all other errors will still be handled by the server if you don't have APACHE and define the new pages.

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, so in your case mod_rewrite would be the best option, but its still basically the same principle as the error document except that mod rewrite doesnt mark documents as errors... The browser assumes everything is ok, so you should then send HTTP headers in the PHP script.


  •  

    Posting Permissions

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