Helpful Information
 
 
Category: Post a PHP snippet
Simple error manager

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

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

Or you could just set your Error 404 page to a page with a QueryString, mod_rewrite wouldn't be needed at all.

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.

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

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)

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 (http://www.alistapart.com/articles/perfect404/) 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!)

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.

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.










privacy (GDPR)