Calling a CGI from within another CGI

Status
Not open for further replies.

beefkirky

Ars Scholae Palatinae
802
I have two CGI's, and I wish to simply pass control to a second CGI from the first.

I don't want to use functions in the second CGI, I wish to say something like

if ( yada yada)
{
do this and this and this;
} else{
go do the second CGI;
}

The second CGI produces a webpage output. So in the first I produce web output, unless something goes wrong, then I want the second CGI to produce its output instead.

I'm using perl to write the CGIs, but I don't know how to pass control around like this.

Can anyone help?
 

bearnhar

Ars Tribunus Militum
1,722
if ( yada yada)
{
do this and this and this;
} else{
print redirect('my_other_cgi.cgi');
}

This is how I did it. You may need to use an absolute URL instead of just the filename. And note that if you do it this way, it needs to do the redirect before it creates the header, so if you create a header before the if (yada yada), you'd need to refresh the header. Since I was careful about when I had it call the redirect, this was a non-issue, but I know it's a common problem people run into with redirects.
 

sroylance

Ars Tribunus Militum
2,156
Is there a reason why not to just implement the logic of the second script in the first script?

code:<HR>
Code:
if (something)
    {
    Do your thing
    }
else
    {
    copy-and-paste this from the other script
    }

If you don't want to do it that way, you can redirect them. The CGI.pm redirect command works by putting a redirect in the http header. When the client gets the header, it gets the document from the redirected-to URL instead. If you've already sent a header, then you can send the client a little javascript that resets the current document to the new page (same effect). The javascript way works much better if you end the html docuement and close the connection ASAP after sending the script. Otherwise the client will spend a bunch of time downloading and rendering the rest of the page, only to carry out the javascript redirect as soon as its done.
 

bearnhar

Ars Tribunus Militum
1,722
What do you mean "the header"?

Are you referring to the HTML headers that are required for the output to be correctly interpreted?
Yup. You can have CGI do a bunch of stuff the user never sees, nothing gets to them until you print the header. There are a bunch of ways to do it. One I use is simply the line
code:<HR>
Code:
print header;
Which automates it. There are a lot of options you can do with this one, just look at the perldocs for CGI. Another is to do it manually:

code:<HR>
Code:
print q ( 
 all the stuff that goes into a regular HTML header as long as it has no parentheses
);
And what exactly is redirect? Some sort of sub procedure I should write? Do I need to "require" some file?

I'm actually not sure what modules you need, but I know they'll be there if you just:

code:<HR>
Code:
 use CGI qw(:standard);

I'm too lazy to really optimize things and limit what modules I use, and using standard covers almost everything you'd likely need almost all the time. And, no, redirect is nothing special. Just tells it where to bounce them, another CGI or an HTML file, wherever you want. But remember, do it [BOLD] before [/BOLD] you print the header, unless you do something wierd like refresh the header, which you should be able to easily avoid having to do.
 

bearnhar

Ars Tribunus Militum
1,722
sroylance's suggestion re: putting them in the same file makes a lot of sense, if the other CGI is something specific to that page.

I was bouncing people to a "how_did_you_get_here_go_back_and_log_in.html" kind of page, so it applied to a lot of pages.

Of course, I could have done it with an include... but the infinite regress of different ways to do the same thing in perl/CGI is not a good place to go just yet :) I only looked at the abyss recently, and don't want to think about it too hard now, my head still spins...
 
I think you should be able to just call the second CGI from the first; everything written to stdout will still be written to stdout, and the environment should still be the same.

I haven't tried this (its been a while since I worked with CGI, but its worth trying at least).


From the sound of it, it doesn't sound like you want to use redirect (this actually sends a redirect header to the browser, which then asks the server for the next page) as this will lose all the input to the CGI.

A better solution would be to rewrite the application, making it modular so you can just import the sections you need to use at a given time.
 
Status
Not open for further replies.