PDA

View Full Version : [MOD] Custom Error Pages


gb
12-13-2005, 01:53 PM
I have implemented custom error pages that are handled by a template. I need to do that for the different languages.

Let me know if this makes sense to you than I will remove the suggested from the title.

1.) Create file in root called "error.php"
Content:


<?php
/************************************************** *************************
*
* IMPORTANT: This program is a commercial software and any kind of using
* it must agree to eSyndiCat license agreement. It can be found here:
* http://www.esyndicat.com/license.html
*
* This notice may not be removed from the source code.
*
* Copyright 2005 eSyndiCat Software Development Ltd.
* http://www.esyndicat.com/
*
************************************************** *************************/
require_once('classes/Dir.php');
require_once('includes/auth.inc.php');
require_once('templates/'.$gDirConfig['tmpl'].'/Layout.php');
require_once('classes/DirSmarty.php');
require_once('util.php');

/** defines page title **/
$gDirSmarty->assign_by_ref('title', $gDirLang['error']);

/** menu display **/
$gDirSmarty->assign_by_ref('menu', $gDirLayout->print_menu());

/** gets error **/
$category =& $gDirDb->getCategoryByPath($_GET['error']);
$gDirSmarty->assign_by_ref('error', $_GET['error']);


/** breadcrumb formation **/
$bc['help']['url'] = '';
$bc['help']['caption'] = $gDirLang['error'];
$breadcrumb = $gDirLayout->print_breadcrumb(0, $bc, 1);
$gDirSmarty->assign_by_ref('breadcrumb', $breadcrumb);

$gDirSmarty->display('error.tpl');
?>


2.) in the .htaccess file in the root directory add the following lines at the end


RewriteRule ^([0-9]+).htm$ error.php?error=$1 [QSA,L]

ErrorDocument 500 /500.htm
ErrorDocument 404 /404.htm
ErrorDocument 403 /403.htm
ErrorDocument 401 /401.htm


3.) Creat a template in your templates folder called "error.tpl"

Content of this file (basic - modify to your own needs):


{include file="header.tpl"}

<h1>{$lang.error} {$error}</h1>


{$error}

{include file="footer.tpl"}


4.) In your language file (ex. English.php) create a new line


'error' => 'Error',


To take this a step further you can create language entries for the error text that should be displayed for the different errors

like:


'404' => 'File not found',
'403' => 'Access Denied',
'401' => 'Unauthorized',
'500' => 'Page not Available',


and include the text dynamically in your error.tpl like


{$lang.$error}


Greetings,
gb

Simon Gooffin
12-14-2005, 04:00 AM
really cool mod, would not you mind if I include it in our default version.

echopulse
12-14-2005, 04:08 AM
It would be cool addition!

Simon Gooffin
12-14-2005, 04:18 AM
I do think so also ;)

gb
12-14-2005, 07:33 AM
not at all. you are welcome. :D

Simon Gooffin
12-14-2005, 07:55 AM
cool. :) thanks again

echopulse
12-30-2005, 05:35 AM
Hi. I have installed this, but I can't get it to change the message for each error. I'd like to have the title and breadcrumb say the error code, and the text have more information for the visitor. The last part of your mod does not work for me.

Simon Gooffin
12-30-2005, 08:56 AM
try to add the following line in your error.php file:

*** open error.php and find the following code ***
$gDirSmarty->assign_by_ref('breadcrumb', $breadcrumb);

*** add after it ***
$gDirSmarty->assign_by_ref('error', $gDirLang[$_GET['error']]);

*** save changes ***

I think you will have it working :) And additional thing. If you have your directory script installed in subdirectory (not html root) you should modify your .htaccess lines also:
ErrorDocument 500 /dir/500.htm
I could make it work after I added subdirectory title

echopulse
12-30-2005, 02:58 PM
That fixed it. Now, I would like to have an additional paragraph on each page that is specific to each error. I'm sure that can be done with if statements, but I'm not sure how to use them. Any help would be appreciated.

gb
12-30-2005, 03:07 PM
you could add in your error.tpl


{assign var=long_error value="long_$error"}
{$lang.$long_error}


in your language file add


'long_404' => 'The server cannot find the file you requested.

The file may have been moved or deleted, or perhaps you entered the wrong url or document name.

You may be able to find the information you were looking for in one of the pages below.',
'long_403' => 'You have tried to reach a page that requires special permission for viewing.

If you have entered a username and password please go back and try again. Remember that both usernames and passwords are case sensitive.

If you continue to have problems with your access, or if you feel you have received this message in error, please contact us.

Alternatively the information you were looking for may be available from one of the other pages on our site.',
'long_401' => 'The server is looking for some encryption key and is not getting it. There may be a problem with the server or, if you have entered a username and password, you may need to go back and try again.

If you continue to have problems with your access, or if you feel you have received this message in error, please contact us.

Alternatively the information you were looking for may be available from one of the other pages on our site.',
'long_500' => 'It was not possible to retrieve the HTML document you were looking for, possibly because of server-configuration problems.

We would really appreciate it if you had time to contact the Webmaster and let us know what you were looking for when the error occurred, so that we can sort out any problems that may have arisen unknown to us.

Or you may prefer just to carry on to one of these pages.',


change the text to whatever you like.

echopulse
01-01-2006, 07:02 PM
Did what you requested. It did not work for me. Below is the contents of my template. Any help is greatly appreciated.

{include file="header.tpl"}

<h1>{$error}</h1>



{assign var=long_error value="long_$error"}
{$lang.$long_error}

{include file="footer.tpl"}

gb
01-01-2006, 09:09 PM
Hi echopulse,

I wont work if you have the line added that simon suggest to add to error.php

$gDirSmarty->assign_by_ref('error', $gDirLang[$_GET['error']]);

I don't see the sense of it anyway as the variable $error is already set here:


/** gets error **/
$category =& $gDirDb->getCategoryByPath($_GET['error']);
$gDirSmarty->assign_by_ref('error', $error);


Try to remove it and let me know the results. I'm using the mod as orginally posted and it works fine.

Simon Gooffin
01-03-2006, 11:18 AM
hi guys,
gb is right :) you will not have it working if you use my code as $error variable has another value not error code.
I do see gb already assigned $error value to smarty 'error' variable but I do not see where you assign real value in php to '$error' php variable. it is empty and equal to nothing.

gb
01-03-2006, 09:51 PM
simon, it's not defined in php, it comes from the URI
see .htaccess line:

RewriteRule ^([0-9]+).htm$ error.php?error=$1 [QSA,L]

Simon Gooffin
01-04-2006, 03:36 AM
sure, it's mod_rewrite rule but to get the value of this error $1 you should use $_GET['error'] I suppose :)

gb
01-04-2006, 06:30 AM
works with me with or without GET. Maybe something in my php.ini
I have updated the mod above (changed $error to $_GET['error']) to be on the safe side anyway.

Simon Gooffin
01-05-2006, 04:32 AM
did not work for me ;) I think there is something different in our server configuration

gb
01-05-2006, 09:53 AM
i have "register_globals" in my php.ini "on", that's why it works.
As this is security risk i will turn it off.

Simon Gooffin
01-06-2006, 05:58 AM
good idea ;) well, now your modification should work fine

Michele
08-02-2006, 10:26 PM
I am trying to follow what's been discussed here.

It appears the mod was added to the pro version I downloaded, but I can't seem to get anything other than a template page with no content other than a breadcrumb with: homepage >> and nothing else.

What am I missing?

Thanks.