How to redirect from one CGI to another

2019-03-04 19:45发布

问题:

I am sending data from A.cgi to B.cgi. B.cgi updates the data in the database and is supposed to redirect back to A.cgi, at which point A.cgi should display the updated data. I added the following code to B.cgi to do the redirect, immediately after the database update:

$url = "http://Travel/cgi-bin/A.cgi/";
print "Location: $url\n\n";
exit();

After successfully updating the database, the page simply prints

Location: http://Travel/cgi-bin/A.cgi/

and stays on B.cgi, without ever getting redirected to A.cgi. How can I make the redirect work?

回答1:

Location: is a header and headers must come before all ordinary output, that's probably your problem. But doing this manually is unneccessarly complicated anyways, you would be better of using the redirect function of CGI.pm



回答2:

Use CGI's redirect method:

my $url = "http://Travel/cgi-bin/A.cgi";
my $q = CGI->new;
print $q->redirect($url);


标签: perl cgi