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

Is there a method in Perl of redirecting a user back from where the original cgi was executed from instead of just redirecting to a predefined page.

Costas

Replies are listed 'Best First'.
Re: redirect back to where you were
by miyagawa (Chaplain) on Jul 25, 2001 at 14:41 UTC
    Do you mean  print CGI::redirect($ENV{HTTP_REFERER});?

    if you wanna make form widgets sticky, HTML::FillInForm would be useful.

    --
    Tatsuhiko Miyagawa
    miyagawa@cpan.org

Re: redirect back to where you were
by Chady (Priest) on Jul 25, 2001 at 14:39 UTC

    why not try the plain old trick of having the cgi generate the form when called without params, and when invoked with param(), it does what it should than call the same sub which generates the form page?

    I hope you are using CGI.pm and beware of sticky fields


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: redirect back to where you were
by earthboundmisfit (Chaplain) on Jul 25, 2001 at 14:45 UTC
    You can issue a redirect via the CGI module:
    use strict; use warnings; use CGI; my $URI = "../html/thePage.html"; my $q = new CGI; print $q->redirect(-uri=>$URI);
Re: redirect back to where you were
by costas (Scribe) on Jul 25, 2001 at 15:28 UTC
    basically i am looking for an equivalent to javascripts history.back.

    However there is a further snag. With javascript i had the history back function planted in the the same html script as the input form. Upon submiting the details, the form details would be processed and then the history.back would be executed taking me back to the page BEFORE the form page.

    With 'print CGI::redirect($ENV{HTTP_REFERER})', because the cgi script is executed then the redirect is executed it itakes me back to the form page and not the page BEFORE.

    In basic terms is there a simple way of jumping back two pages instead of one.

    Costas
      You can jump back two pages if you dynamically generate the form from a cgi script. What I do is leave the electronic equivalent of bread crumbs.

      I put $ENV{'HTTP_REFERER'} into a hidden form field inside my dynamically generated form.

      <form> --------- stuff ---- <input type="hidden" name="backpage" value="$ENV{'HTTP_REFERER'}"> <input type="submit"> </form>
      Now you are free to redirect them in the final, form processing, script to CGI::redirect($input{'backpage'}).

      (Actually, I'm not so sure what CGI.pm puts it's input into, because I don't use it. But you get my meaning.)

      oakbox
      "If what I'm saying doesn't make sense, that's because sense cannot be made, it's something that must be sensed"-J.S. Hall

        I knew about JavaScript trick, but I like more trick with using $ENV{'HTTP_REFERER'} and will use it in my current project. Why? It works also if client disabled JavaScript (as I did recently).
        Quite often I'll also just add plain link (or submit button) like {Back to Main Menu} or {Close All Open Issues}, just to bypass multiple {BACK} commands and start fresh. Sometimes it's easier for users to start afresh.

        pmas
        To make errors is human. But to make million errors per second, you need a computer.

      Let's assume you go with the embedded JS. Simply output the script based on a condition of your post:
      # script head above if($q->param('f') eq 'done'){ print '<SCRIPT Language=\'JavaScript\'> history.go(-1) </SCRIPT>'; }
      In your HTML form simply place a hiden tag:
      <INPUT TYPE=hidden NAME=f VALUE=done>
      Similarly, you could place the CGI::redirect object within this conditional block. Forgive me if this is a simplistic response, but it's not very clear what you're after.
Re: redirect back to where you were
by costas (Scribe) on Jul 25, 2001 at 16:35 UTC
    do you have an alternate way, i am not quite yet a guru.