Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

[Contribution] Custom HTTP Error Pages


hobbzilla

Recommended Posts

Description:

 

This will make any of those ugly 401,404,405, etc. error pages disappear and be replaced with a simple error surrounded by your sexy oscommerce shop. Thus not leaving a potential customer stranded and no place to go!

 

http://www.oscommerce.com/community/contributions,933

 

Well, 17 years later... my first contribution. Hope someone else finds this useful! Let me know what you think!

Link to comment
Share on other sites

  • Replies 51
  • Created
  • Last Reply

Top Posters In This Topic

Ryan;

 

Neat feature, thanks!

 

I am encountering a small problem, though. When it goes to the error page, the text for the error is not showing up:

 

http://www.farmex.now.tc/catalog/http_erro...hp?error_id=404

 

:?:

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

Do you mean in catalog/includes/application_top.php?

For this mod to work, you need to add to

/catalog/application_top.php this bit of code:

 

Code:

define('FILENAME_HTTP_ERROR', 'http_error.php');

 

 

should work after that...

Been there, done that.

 

Have you got this working on your site? I just can't seem to find the solution :?:

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

Jason;

 

I just looked at your site... you're giving the same response that mine is - the text for the error is not showing up.

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

Do you mean in catalog/includes/application_top.php?

For this mod to work, you need to add to

/catalog/application_top.php this bit of code:

 

Code:

define('FILENAME_HTTP_ERROR', 'http_error.php');

 

 

should work after that...

Been there, done that.

 

Have you got this working on your site? I just can't seem to find the solution :?:

 

Sorry, yes! /catalog/includes/application_top.php

 

But..that only fixes part of the problem, we're missing some definitions in our english.php file as well..

Link to comment
Share on other sites

That's what I thought, too. Tried copying all of those definitions into the file and no change (although I didn't clear my browser, I don't think). Maybe I'll go back and try that again.

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

Well, this seems to work, but I don't know if it has other (as yet, unseen) ramifications nor do I know if it eliminates the need for the file that's in the languages/english folder.

 

The contribution file catalog/http_error.php reads:

switch ($HTTP_GET_VARS['error_id']) {

  case '301':  $error_text = ERROR_301_DESC; break;

  case '302':  $error_text = ERROR_302_DESC; break;

  case '400':  $error_text = ERROR_400_DESC; break;

  case '401':  $error_text = ERROR_401_DESC; break;

  case '403':  $error_text = ERROR_403_DESC; break;

  case '404':  $error_text = ERROR_404_DESC; break;

  case '405':  $error_text = ERROR_405_DESC; break;

  case '408':  $error_text = ERROR_408_DESC; break;

  case '415':  $error_text = ERROR_415_DESC; break;

  case '500':  $error_text = ERROR_500_DESC; break;

  case '501':  $error_text = ERROR_501_DESC; break;

  case '502':  $error_text = ERROR_502_DESC; break;

  case '503':  $error_text = ERROR_503_DESC; break;

  case '504':  $error_text = ERROR_504_DESC; break;

  case '505':  $error_text = ERROR_505_DESC; break;

  default:     $error_text = UNKNOWN_ERROR_DESC; break;

If you replace the description references with the actual text, it seems to work OK:

switch ($HTTP_GET_VARS['error_id']) {

  case '301':  $error_text = 'Moved Permanently'; break;

  case '302':  $error_text = 'Moved Temporarily'; break;

  case '400':  $error_text = 'Bad Request'; break;

  case '401':  $error_text = 'Authorization Required'; break;

  case '403':  $error_text = 'Forbidden'; break;

  case '404':  $error_text = 'Not Found'; break;

  case '405':  $error_text = 'Method Not Allowed'; break;

  case '408':  $error_text = 'Request Timed Out'; break;

  case '415':  $error_text = 'Unsupported Media Type'; break;

  case '500':  $error_text = 'Internal Server Error'; break;

  case '501':  $error_text = 'Not Implemented'; break;

  case '502':  $error_text = 'Bad Gateway'; break;

  case '503':  $error_text = 'Service Unavailable'; break;

  case '504':  $error_text = 'Gateway Timeout'; break;

  case '505':  $error_text = 'HTTP Version Not Supported'; break;

  default:     $error_text = 'Unknown Error'; break;

Let me know if I've missed something here :roll:

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

Hey guys, sorry you seem to be having problems with the contrib.

 

The contrib should have included a file:

/catalog/includes/languages/english/http_error.php

 

This file contains all of the:

 

define('ERROR_###_DESC', 'ERROR DESCRIPTION TEXT HERE');

 

It is set up this way and not included in the /catalog/http_error.php file like you guys said you modified to be there instead of the php constant. So, if you change /catalog/http_error.php to look something like:

 

   case '404':  $error_text = 'Not Found'; break;

 

It will ALWAYS be in the english language. You should not do this due to the fact that if the customer selects a different language, or has an installed language in osC set as their default browser language.. it would then be limited to english. This way, if you copy the /catalog/includes/languages/LANG/http_error.php file to another LANG folder besides english, you can modify it so 'Not Found' reads 'No Encontrado' for spanish (if that is in fact 'not found' in spanish.. hey I used babelfish!) I will leave the translations for people that actually know the other language!

 

Let me know if this solves your problem.. or if you catch something I've missed. After all it is my first contribution!

 

Glad you like it!

Link to comment
Share on other sites

Hi, Ryan!

 

I had it set up (I believe) exactly the way your instructions say. I have put the original catalog/http_error.php file back up for you to see the result that I get without making the changes mentioned earlier in this thread.

 

Go to: http://farmex.now.tc/stuff

 

I have the catalog/includes/languages/english/http_error.php in its proper spot and I have amended the catalog/includes/application_top.php file with:

// Custom error pages

 define('FILENAME_HTTP_ERROR', 'http_error.php');

 

Any thoughts?

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

DOH!

 

Okay I guess I'm shooting myself in the foot for this one!

 

I will be updating the contrib to include a fix for this silly mistake.

 

in /catalog/http_error.php, simply move the entire opening switch:case statement BELOW this line:

 

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_HTTP_ERROR);

 

:oops: kinda hard to call constants before they are defined! oops!

Link to comment
Share on other sites

Great, that does it, thanks :!:

 

Silly of PHP to insist on something being defined before it can find it, isn't it :?:

 

Thanks again, great addition

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

This is a nice script, but only the customer see the bug. It would be better if the webmaster where notified about an error.

 

We use a small script from

 

http://www.rustyparts.com/scripts.php

 

Only one file and two changes before upload. The script sent a mail to the admin with the error code, the url and the referer like this

 

------------------------------------------------------------------------------

Site: xxx.com (www.xxx.com)

Error Code: 404 Missing URL (File does not exist: /www/htdocs/fetish/www.xxx.com/shop/ban-pic.jpg)

Occurred: Sat Feb 1 16:50:52 EST 2003

Requested URL: /shop/ban-pic.jpg

User Address: 213.10.122.148

User Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)

Referer: http://www.xxx.com/rank/index.php

------------------------------------------------------------------------------

 

The only thing is, the script shoes his nown error page. When the script shows the shop page after an error. This would be perfect.

 

Or add an notification to this add on.

 

 

This is our script. If someone know the changes to show the shop site after an error...........?

 

 

<?php

/*-------------------------------------------------------------*

| phpErrorDoc written by jason rust <[email protected]>

|

| v1.0 De. 29 2001 - first version

|

| from http://www.rustyparts.com/scripts.php

| under the GNU license you are free to use/modify this script

| email me any improvements and want them included in the next

| version.

| this script comes with no implied warranty.

|

| If you want to send me a thank you, starbucks coupons

| are gladly accepted

| (http://www.starbucks.com ;)

| my address is:

| attention: jason's phpErrorDoc

| 212 Cottage Grove Ave.

| Santa Barbara, CA 93101

| otherwise it's free!

|

| Cheers!

| -Jason

*-------------------------------------------------------------*/

#

# This is where all of the various parameters for the script are

# set.

#

#The e-mail address of the person to notify when an error occurs.

$notify="[email protected]";

 

#The name of your site

$sitename="xxx.com";

 

#The link the reader should follow home.

$returnLink="http://www.xxx.com/shop/catalog/";

 

# the full path to the error log. make sure it is writable

$errorLog = "/path/to/error_log";

 

# colors for the tables error page below

# background color of the page

$body_bg_color = "#ffffff";

# background color of the table

$table_bg_color = "#dedef6";

# text color of the table

$table_fg_color = "#000000";

# background color of the cell on the left with a image

$image_bg_color = "#ffffff";

# color of the header text

$header_color = "#DD0101";

 

# images to display on left hand side (full path to the image)

$image = array (

'000' => '/shop/catalog/images/infobox/error.gif',

'400' => '/shop/catalog/images/infobox/error.gif',

'401' => '/shop/catalog/images/infobox/error.gif',

'403' => '/shop/catalog/images/infobox/error.gif',

'404' => '/shop/catalog/images/infobox/error.gif',

'500' => '/shop/catalog/images/infobox/error.gif'

);

 

#These are the Subject Lines for the e-mail notification

#You can modify these without causing any problems.

$subject = array (

'000' => 'Unknown Error',

'400' => 'Bad Request',

'401' => 'No Authorization',

'403' => 'Forbidden URL',

'404' => 'Missing URL',

'500' => 'Configuration Error'

);

 

#Change the N to Y if you want to receive e-mail when a particular error occurs.

$email = array (

'000' => 'Y',

'400' => 'Y',

'401' => 'Y',

'403' => 'Y',

'404' => 'Y',

'500' => 'Y'

);

 

#Change the N to Y if you want to log a particular error to file

$log = array (

'000' => 'N',

'400' => 'N',

'401' => 'N',

'403' => 'N',

'404' => 'N',

'500' => 'N'

);

 

###################################################################

#

# edit the html error messages below to suit your needs.

#

#HTML CODE TO APPEAR WHEN A BAD REQUEST OCCURS

$msg['400'] = "

<b>The URL that you requested, $REDIRECT_URL

was a bad request.</b>";

 

##################################################################

#HTML CODE TO APPEAR WHEN AN UNAUTHORIZED PAGE ACCESS ATTEMP OCCURS

$msg['401'] = "

<b>The URL that you requested, $REDIRECT_URL

requires preauthorization to access.</b>";

 

##################################################################

#HTML CODE TO APPEAR WHEN A FORBIDDEN ATTEMPT IS MADE

$msg['403'] = "

<b>Access to the URL that you requested, $REDIRECT_URL,

is forbidden.</b>";

 

##################################################################

#HTML CODE TO APPEAR WHEN A DOCUMENT NOT FOUND HAPPENS

$msg['404'] = "

<b>The URL that you requested, $REDIRECT_URL,

could not be found. Perhaps you either mistyped the

URL or we have a broken link.

<br /><br />

We have logged this error and will correct the

problem if it is a broken link.</b>";

 

##################################################################

#HTML CODE TO APPEAR WHEN A SERVER CONFIGURATION ERROR OCCURS

$msg['500'] = "

<b>The URL that you requested, $REDIRECT_URL

resulted in a server configuration error. It is

possible that the condition causing the problem will

be gone by the time you finish reading this.

<br /><br />

We have logged this error and will correct the

problem.</b>";

 

##################################################################

#HTML CODE TO APPEAR WHEN AN UNKNOWN ERROR OCCURS

$msg['000'] = "

<b>The URL that you requested, $REDIRECT_URL

resulted in an unknown error code.

It is possible that the condition causing the problem will

be gone by the time you finish reading this.

<br /><br />

We have logged this error and will correct the

problem.</b>";

 

### you shouldn't need to change anything below here ###

 

$result = $QUERY_STRING;

if ($result != "400" && $result != "400" && $result != "403" && $result != "404" && $result != "500") $result="000";

 

print_header($result);

echo $msg[$result];

print_footer();

 

if ($log[$result] == "Y") notify("L",$result);

if ($email[$result] == "Y") notify("M",$result);

 

##################################################################

# this routine sends an e-mail or writes to a log depending

# on whether it was called with an "L" or "M"

function notify ($action, $result)

{

global $errorLog, $subject, $notify, $sitename, $REQUEST_URI, $REMOTE_ADDR, $HTTP_USER_AGENT, $REDIRECT_ERROR_NOTES, $SERVER_NAME,$HTTP_REFERER;

$date=date("D M j G:i:s T Y");

 

 

# see what action to take

if ($action == "L") {

$message = "[$date] [client: $REMOTE_ADDR ($HTTP_USER_AGENT)] $REDIRECT_ERROR_NOTESn";

$fp = fopen ($errorLog,"a+");

fwrite($fp, $message);

fclose($fp);

} else {

# create message

$message = "

------------------------------------------------------------------------------

Site:tt$sitename ($SERVER_NAME)

Error Code:t$result $subject[$result] ($REDIRECT_ERROR_NOTES)

Occurred:t$date

Requested URL:t$REQUEST_URI

User Address:t$REMOTE_ADDR

User Agent:t$HTTP_USER_AGENT

Referer:t$HTTP_REFERER

------------------------------------------------------------------------------";

mail("$notify", "[ Server Error: $subject[$result] ]", $message,

"From: server_error@$SERVER_NAMErn"

."X-Mailer: PHP/" . phpversion());

}

}

 

function print_header($result)

{

global $subject, $body_bg_color, $table_bg_color, $table_fg_color, $image_bg_color, $image, $header_color, $sitename, $REDIRECT_ERROR_NOTES;

# take off the path to the script, we don't want them to see that

$error_notes = preg_replace("/:.*/","",$REDIRECT_ERROR_NOTES);

if (empty($error_notes)) $error_notes = "Unknown";

echo "

<html>

<head>

<title>$subject[$result]</title>

<style>

a:link,a:active,a:active {

font-weight: bold;

text-decoration: none;

color: blue;

}

a:hover {

font-weight: bold;

text-decoration: underline;

color: blue;

}

</style>

</head>

</html>

<body bgcolor="$body_bg_color">

<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">

<tr>

<td valign="center">

<table width="550" bgcolor="$table_bg_color" border="1" align="center" cellspacing="0" cellpadding="5" color="$table_fg_color">

<tr>

<td bgcolor="$image_bg_color">

<img src="$image[$result]" alt="error">

</td>

<td>

<center>

<span style="font-weight: 600; color: $header_color;">$sitename</span> <b>Error $result</b>

<br />

($error_notes)

<br />

<br />

<center><b>Please <a href="$returnLink">Click Here</a> to Return to the Shop.</b></center>

<br />

</center>

<br />";

}

 

function print_footer()

{

global $returnLink;

echo "

</td>

</tr>

</table>

</td>

</tr>

</table>

</body>

</html>";

}

?>

Link to comment
Share on other sites

OK, now I've got a new problem.

 

Used to be that I could go to http://farmex.now.tc/catalog

When I try that now, I get a (301) Moved permanently error, but if I try to go to http://farmex.now.tc/catalog/ (note the trailing slash), it works fine.

 

What in this mod has changed that behavior?

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

A simple and useful contribution. Thanks.

 

I was getting the above 301 error and simply removed the handlers for 30X errors. Now all is behaving. I assume it has to do with Apache's settings?

 

I will be implementing the above suggestion re: email notifications and will post my progress.

 

Thanks,

 

Greg

Link to comment
Share on other sites

mugitty:

 

You should simply remove the line from .htaccess that states:

 

ErrorDocument 301 http://www.DOMAIN.com/catalog/http_error.php?error_id=301

ErrorDocument 302 http://www.DOMAIN.com/catalog/http_error.php?error_id=302

 

Apparently it is not playing nice with Apache's rewrite rules. This is due to the fact that you don't have a "catalog" FILE in your directory. The *real* path should in fact be .../catalog/ (with the trailing slash) but since that is cumbersome.. they made rewrite rules just for this purpose.

 

I will delete this and add a new contribution that includes admin options to e-mail or file the error type & referral information to the store owner.

Link to comment
Share on other sites

Thanks again, Ryan, all better now.

Looking forward to the update!

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

OK, installed v1.2!

 

Seems to be working fine. A couple of notes-

 

in catalog/includes/languages/english/http_error.php:

1) Made a minor grammatical change (removed 'are' from We are apologize for any inconvenience) and

2) Added 2 line breaks at the end of that same sentence to move the name of the attempted file down to its own line.

 

elsewhere:

3) It may be assumed, but I don't think it says in the instructions to set the permissions to write on the log file.

 

It's a peach, thanks, Ryan :!:

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

...and the email function is working fine-

 

I just got about ten attempts from this guy in the last 3 minutes

 

Site: http://farmex.now.tc.

Error Code: 404 - File Not Found

Occurred: 02/02/2003 17:08:35

Requested URL: http://farmex.now.tc/scripts/..%c1%9c../wi.../cmd.exe?/c+dir

User Address: 68.56.93.111

User Agent:

Referer:

 

these people REALLY need to get a life!

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,

try being a Foster Parent!

Link to comment
Share on other sites

Yeah.. I don't use spell checker or anything like that.. I will make a note of it on my local copy for the next build (if there is one).

 

I honestly don't know how Apache handles logging errors when you invoke the ErrorDocument directive. I am thinking that it still stores it in the regular access log which makes my additions a little redundant. I peraonally don't want to get an e-mail everytime someone is checking if I haven't patched my IIS server (which I'm running apache). But the addition was requested and easily added.

 

Glad you finally got it all working!

Link to comment
Share on other sites

I've got the newer version installed, but I keep getting the same value for the requested URL: the error page itself:

------------------------------------------------------

Site: http://www.magshack.com.

Error Code: 404 - Not Found

Occurred: 02/03/2003 20:15:00

Requested URL: http://www.magshack.com/http_error.php?error_id=404

User Address: 207.###.###.94

User Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)

Referer:  

------------------------------------------------------

If you go to the above site, each page has a bum link (HTTP Error Test) in the footer for testing.

 

Any suggestions will be appreciated.

 

Thanks,

 

Greg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...