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

I have a page that calls a from after users have put in their information in the form, my CGI sript is supposed to insert the information in a database and redirect them back to the original web page. How do I redirect back to the original web page?? Im using perl 5 I tried print redirect ("name.html"); but it didnt work
  • Comment on how do I redirect to a previous page in my CGI script

Replies are listed 'Best First'.
(wil) Re: how do I redirect to a previous page in my CGI script
by wil (Priest) on May 14, 2002 at 10:08 UTC
    Redirecting the user to the previous page using CGI.pm should be failry straightforward. Here's some sample code that should give you an idea on how to go about this.
    #!/usr/bin/perl use strict; use CGI; my $query = CGI->new(); # code to enter records into database goes here. my $referrer = $ENV{HTTP_REFERER}; print $query->redirect($referrer);

    Hope this helps.

    - wil
Re: how do I redirect to a previous page in my CGI script
by hotshot (Prior) on May 14, 2002 at 09:18 UTC
    You can try putting:
    print header(-location=>'name.html');
    After you update your databas

    Thanks.

    Hotshot
Re: how do i redirect to a page in my CGI script
by adamcrussell (Hermit) on May 14, 2002 at 12:49 UTC
    At the appropriate point in your CGI have a print qq(<meta HTTP-EQUIV="REFRESH" URL="someurl" CONTENT="0"/>);
    That is how you would do it if you aren't using CGI.pm which you should be actually. Using CGI.pm you would do this:
    print $query->redirect(-uri=>'http://somewhere.else/in/movie/land', -nph=>1);
    I just thought you might like to see both ways to get a better idea of what you are working with. Bottom line is that you should use CGI.pm.
Re: how do I redirect to a previous page in my CGI script
by cfreak (Chaplain) on May 14, 2002 at 13:25 UTC

    Printing the header yourself also works:

    print "Location:name.html\n\n";

    Chris

    Some clever or funny quote here.
      Officially, the Location header is like all other headers, and requires a space after the colon. If it happens to work without that for a particular setup, the setup is error correcting your mistake, and you'll be burned the day you move to a setup that doesn't do such error correction.

      Also, you should be aware of the difference between a CGI script issuing an internal redirect versus an external redirect. Without a HTTP scheme (as in your example), an internal redirect will change the served page, but not the browser's idea of the served page. This has implications with regard to error messages and relative URLs. More often, what you want is an external redirect, which causes the browser to refetch the page at the new address.

      -- Randal L. Schwartz, Perl hacker