gmacfadden has asked for the wisdom of the Perl Monks concerning the following question:

Hope this is better format How does one employ CGI redirect in such a way that it will work within the fabric of other scripts?
While the below code functions correctly, from a practical standpoint it is severely restricted in it's usefulness.

Why is that you ask?

In order to offer the client a greeting (and of course a choice if s/he wants do perform a download), we need to allow plain text/html to go to STDOUT versus the present, but very limited standalone functionality supporting no user context at all. I welcome your ideas please
#! /usr/bin/perl -wT use strict; use CGI qw(:standard escape escapeHTML); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); #all diagnostic t +o browser my $url = 'http://www.gmacfadden.com/cgi-bin/WorkControl/Home/download +_file_from_file.cgi'; # any absolute URL print redirect(-location => $url); exit (0);

Replies are listed 'Best First'.
Re: redirect in more than standalone mode
by friedo (Prior) on Nov 17, 2006 at 20:42 UTC

    CGI's redirect method is a shortcut which outputs a full set of HTTP headers including a Location: line. If you want to display some HTML while having a redirect header, you can do something like the following:

    use strict; use CGI; my $q = CGI->new; print $q->header( -type => 'text/html', -location => 'http://somewhere.com/' ); print "<h1>Here's some HTML.</h1>";
      Thanks for helping.

      OK, going with the OO approach, the redirect to the other script works, but I still don't get "here's some HTML" to print.

      Here's what I did.
      #! /usr/bin/perl -wT use strict; use CGI qw(:standard escape escapeHTML); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); #all diagnostic t +o browser #print header (), # generate Content-Type header #start_html (-title => "Test redirect"); my $q = CGI->new; print $q->header( -type => 'text/html', -location => 'http://www.gmacfadden.com/cgi-bin/Work +Control/Home/download_file_from_file.cgi'); print "<h1>Here's some HTML.</h1>"; #print end_html (); # generate closing HTML tags exit (0);
        Try running that from the command line, and you'll see it print your HTML after the redirection header lines. Whether or not the HTML will appear in a web browser will depend upon many things, among them:
        • Is the browser picky about the absence of <head> and <body>?
        • Does the browser bother to show the content if the redirection succeeds?
        • Does the browser have time to render the content before the server that receives the request from the redirect starts talking?

        What it will not do for you, though, is present you with the option to do your download. The redirect is unconditional. If instead you want to present a hyperlink to the download URL, then just do that without bothering with a redirection header.

Re: redirect in more than standalone mode
by Sidhekin (Priest) on Nov 17, 2006 at 23:51 UTC

    If the browser supports Netscape's "refresh" headers (and what browser doesn't, these days?), you can give it a few seconds in which to render the page before redirecting.

    To demonstrate, here's a variation of friedo's code:

    #!/usr/bin/perl use strict; use warnings; use CGI; my $q = CGI->new; my $delay_seconds = 4; my $url = 'http://somewhere.com/'; print $q->header( -type => 'text/html', -refresh => "$delay_seconds; $url", ); print "<h1>Here's some HTML.</h1>";

    Tested with Firefox and Apache.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!