PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Feb 23, 2020 3:15 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Aug 29, 2011 3:19 am 
Offline
Forum Commoner

Joined: Wed Feb 17, 2010 12:28 am
Posts: 37
What is a good way of handling various error messages on the website?

For example,

1. my site allows user login, if user keys in wrong username/password, display error message, prompt to re-login
2. session exceeds 30mins of inactivity, auto logs out, and user comes back to the computer and tries to navigate, display error message, prompt to re-login
3. database down when try to search any database, redirect to error page, or display error message somehow.
4. access denied, due to lack of sufficient permission to perform action/access a certain page. For example, someone typing in a direct url to my staff pages, fail access check, redirect.

and so on.

I was looking at a website I created 2 years ago and saw this problem of having one error page for one error, ending up with tons of different pages to handle all sorts of error messages. Like I'd have, staff_access_denied.html, not_found.html, failed_connect.html, access_expired.html etc.

Very messy :P

I was thinking about having one error page, every error redirects to that page, and on that one page, I have a switch to display error messages based on the error code I'd include in the redirected url.

How does that sound? Are there better solutions?

Thanks!


Top
 Profile  
 
PostPosted: Tue Aug 30, 2011 8:46 am 
Offline
Forum Commoner

Joined: Sat Aug 27, 2011 11:16 pm
Posts: 33
In the case of #1 I typically have that as an on-page error so that they stay on the same page but a div is inserted with the desired error message.

For #2 this can be achieved by use of cookies or session expirations, or more common with banks, a javascript count down timer or a meta tag with refresh and an interval specified that upon 30 minutes of inactivity (or whatever you decide) the page is automatically redirected to a timeout page or back to the login page. An example can be found here (http://www.daniweb.com/web-development/ ... ads/124500).

As to the rest, having a single page will require that you code a potentially large if then else or switch to account for the various errors, thus it can easily get complicated. However, if it were me I would have an includes folder with all the various error messages where the file name included the code passed in the redirect. For example:

errors/error_1.inc.php, errors/error_2.inc.php, errors/error_3.inc.php, etc.
error_0.inc.php
Syntax: [ Download ] [ Hide ]
<?php
        $error_title = 'Ooops!';
        $error_message = "<div class='error'><h1>Ooops!</h1><p>It appears there was an error!</p></div>";
?>
 

Or if you want to get very fancy:
Syntax: [ Download ] [ Hide ]
<?php
        $error_title = 'Ooops!';
        ob_start(); // Start the output buffer
?>
<div class='error'>
        <h1>Ooops!</h1>
    <p>It appears there was an error!</p>
</div>
<?php
        $error_message = ob_get_clean(); // Dump the output buffer to the variable
?>
 

Then my page would look something like:
Syntax: [ Download ] [ Hide ]
<?php
        if($_GET['error'] && file_exists('errors/error_'.$_GET['error'].'.inc.php')) // Check if the error code was specified, if so check that an error file exists for that code
                include('errors/error_'.$_GET['error'].'.inc.php'); // Include the specified error file
        else
                include('errors/error_0.inc.php'); // Default error to display if no error code was specified or if the error file doesn't exist
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $error_title; ?></title>
</head>

<body>
<?php echo $error_message; ?>
</body>
</html>


Hope this helps!


Top
 Profile  
 
PostPosted: Tue Aug 30, 2011 9:08 am 
Offline
Forum Commoner

Joined: Sat Aug 27, 2011 11:16 pm
Posts: 33
Oops, forgot to add that when you redirect to the error page (e.g. http://www.yoursite.com/error.php?error=2) then it will check for and include the appropriate error include file (e.g. error_2.inc.php). If you specify error 22 and one doesn't exist then it displays the default error_0.inc.php. :D I know I documented this in the code, but wanted to also specify it here as well. :D


Top
 Profile  
 
PostPosted: Wed Aug 31, 2011 12:36 am 
Offline
Forum Commoner

Joined: Wed Feb 17, 2010 12:28 am
Posts: 37
That'd work well for me I believe :)

Very informative tutorial! Thanks a bunch!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group