Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-09-2005, 09:38 PM   PM User | #1
JamieR
Senior Coder

 
JamieR's Avatar
 
Join Date: Oct 2004
Location: United Kingdom
Posts: 3,161
Thanks: 0
Thanked 5 Times in 5 Posts
JamieR is on a distinguished road
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.
__________________
-Jamie
JamieR is offline   Reply With Quote
Old 12-09-2005, 09:44 PM   PM User | #2
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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
Element is offline   Reply With Quote
Old 12-10-2005, 12:09 AM   PM User | #3
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
Or you could just set your Error 404 page to a page with a QueryString, mod_rewrite wouldn't be needed at all.
missing-score is offline   Reply With Quote
Old 12-10-2005, 12:46 AM   PM User | #4
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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.
Element is offline   Reply With Quote
Old 12-10-2005, 04:19 AM   PM User | #5
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
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).
missing-score is offline   Reply With Quote
Old 12-10-2005, 09:26 AM   PM User | #6
JamieR
Senior Coder

 
JamieR's Avatar
 
Join Date: Oct 2004
Location: United Kingdom
Posts: 3,161
Thanks: 0
Thanked 5 Times in 5 Posts
JamieR is on a distinguished road
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)
__________________
-Jamie
JamieR is offline   Reply With Quote
Old 12-12-2005, 08:41 AM   PM User | #7
zro@rtv
Regular Coder

 
zro@rtv's Avatar
 
Join Date: Feb 2005
Location: on the network
Posts: 433
Thanks: 0
Thanked 1 Time in 1 Post
zro@rtv is an unknown quantity at this point
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
zro@rtv is offline   Reply With Quote
Old 12-12-2005, 05:36 PM   PM User | #8
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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.
Element is offline   Reply With Quote
Old 12-12-2005, 05:47 PM   PM User | #9
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
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.
missing-score is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:44 PM.


Advertisement
Log in to turn off these ads.