You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Jonathan M. Hollin" <ne...@digital-word.com> on 2002/05/16 12:40:49 UTC

Force a 404 error?

Hi JAPHs,

Is it possible to force a 404-error from within a mod_perl CGI?

I am working on my CMS and I want to generate a 404 if the user hacks
the URI in a certain way: i.e, if the use passes a value in the URI that
does reference a database record.  I can (and have) trapped such an
event and thus I can display the appropriate "Not found" warning by
using a CGI.pm redirect to my 404 page, but using redirect obviously
updates the URL (as displayed on the browsers address line) to that of
the 404 page, whereas I want the URL to continue to display the users
request.

Can I do this?


Jonathan M. Hollin - WYPUG Co-ordinator

West Yorkshire Perl User Group
http://wypug.pm.org/
http://wypug.digital-word.com/


Re: Force a 404 error?

Posted by Helios de Creisquer <he...@balios.org>.
On Thu, May 16, 2002 at 11:40:49AM +0100, Jonathan M. Hollin wrote:
> Hi JAPHs,
> 
> Is it possible to force a 404-error from within a mod_perl CGI?
> 
> I am working on my CMS and I want to generate a 404 if the user hacks
> the URI in a certain way: i.e, if the use passes a value in the URI that
> does reference a database record.  I can (and have) trapped such an
> event and thus I can display the appropriate "Not found" warning by
> using a CGI.pm redirect to my 404 page, but using redirect obviously
> updates the URL (as displayed on the browsers address line) to that of
> the 404 page, whereas I want the URL to continue to display the users
> request.
> 
> Can I do this?

See:

	http://archive.covalent.net/modperl/2001/04/0088.xml

Cheers,
-- 
               Helios de Creisquer      <he...@balios.org>
http://www.tuxfamily.org/                        <cr...@tuxfamily.org>
http://www.vhffs.org/      +33 (0)6 70 71 20 29      <cr...@vhffs.org>
http://www.gnu.org/                                    <cr...@gnu.org>
GPG(1024D/96EB1C44): FB11 8B80 4D86 D9C2 DE0C 11D7 2FA8 A5CC 96EB 1C44

RE: Force a 404 error?

Posted by "Jonathan M. Hollin" <ne...@digital-word.com>.
>See Chapter 4, "Handling Errors", in the Eagle book.

Great Thomas, I have a working handler now.  Thanks very much to Thomas
and everyone else who replied with suggestions.


Jonathan M. Hollin - WYPUG Co-ordinator

West Yorkshire Perl User Group
http://wypug.pm.org/
http://wypug.digital-word.com/


Re: Force a 404 error?

Posted by Thomas Klausner <do...@zsi.at>.
Hi!

On Thu, May 16, 2002 at 11:40:49AM +0100, Jonathan M. Hollin wrote:
> Is it possible to force a 404-error from within a mod_perl CGI?
What about returning NOT_FOUND from your handler and let an ErrorDocument
print out the warning?

See Chapter 4, "Handling Errors", in the Eagle book.

-- 
 D_OMM      +---->  http://domm.zsi.at <-----+
 O_xyderkes |       neu:  Arbeitsplatz       |   
 M_echanen  | http://domm.zsi.at/d/d162.html |
 M_asteuei  +--------------------------------+



Re: Force a 404 error?

Posted by Mark Fowler <ma...@twoshortplanks.com>.
On Thu, 16 May 2002, Jonathan M. Hollin wrote:

> Is it possible to force a 404-error from within a mod_perl CGI?

I'm not sure what you mean by a "mod_perl CGI."  Anyway, basically you
need to return the "404" error code in the HTTP response and provide some
helpful HTML.

In CGI this can be done like so:

#!/usr/bin/perl -wT

use strict;

use CGI;
my $q = CGI->new();

# okay, send the header so the browser knows it's a 404
print $q->header(-status => "404");

# now print something that looks like a normal 404 page
my $url = $ENV{REQUEST_URI};
$url = $q->escapeHTML($url);  # be paranoid re cross site scipting

print <<ENDOFHTML;
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
The requested URL $url was not found on this server.<p>
<hr>
<address>$ENV{SERVER_SOFTWARE} at $ENV{SERVER_NAME} Port 
$ENV{SERVER_PORT}</address>
</body></html>
ENDOFHTML

-- 
s''  Mark Fowler                                     London.pm   Bath.pm
     http://www.twoshortplanks.com/              mark@twoshortplanks.com
';use Term'Cap;$t=Tgetent Term'Cap{};print$t->Tputs(cl);for$w(split/  +/
){for(0..30){$|=print$t->Tgoto(cm,$_,$y)." $w";select$k,$k,$k,.03}$y+=2}