[CGI] How to set a 404 error?

R

Rui Maciel

In a perl CGI script, is it possible to throw a 404 error? If so, how is it done?


Thanks in advance,
Rui Maciel
 
R

Rui Maciel

Keith said:
Read the CGI module docs under "CREATING A STANDARD HTTP HEADER".

I've tried that and failed to get a 404 error page. According to that section of the documentation,
the header() function has an optional parameter to specify status codes in the following manner:

print header('text/html','404 Not found');

Yet, a perl CGI script that runs that function fails to set a 404 error page on the browser, which
was the expected outcome.

So, can anyone provide any clues on how to write a CGI script that sets a 404 error so that the
browser displays it's default 404 page?


Thanks in advance,
Rui Maciel
 
R

Rui Maciel

Sherm said:
You expected a page? Did you print one?


Ah, I see the misconception now. Browsers don't have default 404 pages,
servers do. The browser will display whatever content is sent after the
headers.

Thanks for the help, Sherm. I believed that the standard 404 pages that usually pop up were pre-
defined pages presented by the browser then a 404 error was returned and that the fancy 404 error
pages were soft 404s.

So to make matters clear, in essence a 404 page is created by printing a HTTP response with a 404
status code followed by a set of entity header fields and a document informing the users of a 404
error, is it?


Once again thanks for the help,
Rui Maciel
 
P

Peter J. Holzer

Yes, exactly. The document can be as simple or as fancy as you like.
If you want to mimic your server's default document, just hit a non-
existent URL on your server, and save the HTML it sends in response.

It's interesting to note that what is displayed depends on the browser.

Firefox seems to treat a 404 response exactly like a 200 response: It
just displays the content sent by the server.

IE and Google Chrome display a browser specific error page instead
(but if I remember correctly IE does this only if the error page sent by
the server is very short).

hp
 
R

Rui Maciel

Peter said:
It's interesting to note that what is displayed depends on the browser.

Firefox seems to treat a 404 response exactly like a 200 response: It
just displays the content sent by the server.

IE and Google Chrome display a browser specific error page instead
(but if I remember correctly IE does this only if the error page sent by
the server is very short).

It appears you are correct. While on Chromium 5 and Firefox it rendered a blank window, on
Konqueror 4.4.2 it presented a browser-specific default 404 error page. I thought that all browsers
presented the user with a default 404 page but it appears that that is only wishful thinking.


Rui Maciel
 
P

Peter J. Holzer

This then begs the question, what are you trying to accomplish in
sending a 404 response to the client?

Telling the client that the requested document isn't here?

hp
 
R

Rui Maciel

Keith said:
This then begs the question, what are you trying to accomplish in
sending a 404 response to the client?

I'm exploring the idea of generating different pages according to the values passed through the
PATH_INFO environment variable. More specifically, the valid PATH_INFO values lead the script to
generate pages while the invalid PATH_INFO values lead the script to send a 404.


Rui Maciel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top