hazel-rah has asked for the wisdom of the Perl Monks concerning the following question:

After using a form to get some info from the user, the script validates the info. If it's good, then the script prints out an "OK" message. But instead of printing out a message, I want the script to redirect the browser to another page (also a CGI script), and ideally pass along some variables to that script as well.

I've checked the perldocs for CGI.pm and searched the web, including almost every Q&A under CGI here at perlmonks, but I can't find out how to do this. I feel like I'm missing something painfully obvious here. Any ideas?

My best success so far is to use exec() but it always returns me to the first script when I perform any action on the second one, and that's no good.
  • Comment on How do I make one CGI script send the user to another CGI script?

Replies are listed 'Best First'.
Re: How do I make one CGI script send the user to another CGI script?
by voyager (Friar) on Mar 30, 2001 at 20:08 UTC
    CGI.pm offers a redirect function
    use CGI; my $url = 'myotherscript.pl?parm1=value1&etc=etc'; print CGI::redirect($url);
    As another answer noted, do this before any "print header" or similar
Re: How do I make one CGI script send the user to another CGI script?
by KrYo (Initiate) on Mar 30, 2001 at 14:04 UTC
    This should work.
    use CGI;
    $q = new CGI;
    
    $urlPath = 'http://www.disney.com:88/something.html';
    
    print $q->header(-target=>'_top',-nhp=>1,-expires=>'now',-location=>$urlPath);
    
Re: How do I make one CGI script send the user to another CGI script?
by Xxaxx (Monk) on Mar 30, 2001 at 15:23 UTC
    If the second receiving script can accept variables using the "get" method then you might be able to use location to redirect the browser to the next script.

    my $anotherpage = 'http://www.domain.com/cgi-bin/script.pl'; my $passeddata = "?var1=$data1&var2=$data2"; .... # fixup $passeddata for proper sending print "Location: $anotherpage$passeddata\n\n";
    When using Location is needs to be the first element print.
    Also your data will need to be fixedup so that it will pass correctly. I use the following. I'm quoting it here only to indicate the direction to take and hoping someone will post a more proper conversion. I can't speak for this being complete. It just works in a limited situation I'm using.

    $passeddata =~ s/([!@#$%^&()_+\n])/sprintf("%%%02x",ord($1))/eg; $passeddata =~ s/\s/+/g;
    This kind of solution might work okay for simple confirmation screens, etc.

    I'm not too hot on its use for passing large amounts of data and it's certainly not that secure.

    Claude

Re: How do I make one CGI script send the user to another CGI script?
by hazel-rah (Initiate) on Mar 30, 2001 at 15:12 UTC
    Thanks for the assistance. Is it required that I make a distinct CGI object for the code to work? Because I can't get it to work using just the default CGI object (I'm not sure how to put it... I'm not using the "$q->" syntax). It just prints out the header onto the logon page and doesn't redirect.

    I guess my more general question is "how do you pass information between different CGI scripts?" I assumed some kind of function would exist in CGI.pm that would redirect the browser to a URL given as an argument, and I could just tack on some "?info=foo&info=bar" on the end of it by interpolating variables into the URL.

    Sort of like... create a CGI script with one text box where you type a one or a zero, and then when you click the submit button, the script looks at the parameter it got from the text box, and then sends you to another CGI script via a URL according to what you typed. /~me/decide.cgi sends you to /~me/one.cgi if textbox param returns true, and to /~me/zero.cgi if it doesn't.

    I'm going to sneak off to the bookstore tomorrow and see if I can't find a solution in a CGI/Perl book. I'll post if I find something good.