Forum Moderators: coopster & phranque

Message Too Old, No Replies

Server Side Include on a CGI page

How do you do this?

         

RobbieD

3:23 am on Aug 4, 2003 (gmt 0)

10+ Year Member



Hello,

I am using server side includes on all my static pages but I would like a way to bring this info in on a cgi page.

The code I use is

<!--#include virtual="/cgi-bin/sample.cgi" -->

Works great for static pages the problem is when I use this on a dynamic page.

What would I use to bring in sample.cgi on a dynamic page?

moltar

3:33 am on Aug 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot directly use SSI in Perl scripts.

You can go around this by writing your own function to do the trick.

I use templates for my CGIs. All the HTML is stored separately. So what I do is, I request the page from the server with CGI script and then parse and substitute all the variables in it after.

You can do similar, just request the page from your own server and then display HTML.

Take a look at LWP module.

[edited by: moltar at 3:35 am (utc) on Aug. 4, 2003]

RobbieD

3:34 am on Aug 4, 2003 (gmt 0)

10+ Year Member



So how would I get sample.cgi content to appear on a dynamic page?

Thanks for your help.

moltar

3:38 am on Aug 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




#!/usr/bin/perl

use LWP::Simple;
my $cgi_html = get("http://www.yoursite.com/cgi-bin/sample.cgi");

print '<html><head><title>example</title><body>';
print $cgi_html;
print '</body></html>';

RobbieD

3:43 am on Aug 4, 2003 (gmt 0)

10+ Year Member



Thanks Moltar,

Just one last thing. What if sample.cgi is just one small element in a page. You would have to program this code into the rest of the dynamic page with all the other elements?

Say sample.cgi is just a navbar from another program you want to insert into a dynamic page that already has coding for the Header Body Footer etc.

Thanks

moltar

3:48 am on Aug 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, I think you would have to do that. Unless there are any better solutions that anybody else knows about?

That is what I do right now. I did a little research and unfortunately couldn't find anything better.

I don't understand why wouldn't Apache let SSI work from dynamic pages. What stops it? Anyone?

jamesa

8:08 am on Aug 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't understand why wouldn't Apache let SSI work from dynamic pages. What stops it? Anyone?

It's just a setting in the Apache configuration that determines what file extension(s) will get passed through the SSI engine. So it could be done, by why would you want to on a CGI page when you could just use the CGI language itself to do it?

RobbieD

9:09 am on Aug 4, 2003 (gmt 0)

10+ Year Member



It's just a setting in the Apache configuration that determines what file extension(s) will get passed through the SSI engine. So it could be done, by why would you want to on a CGI page when you could just use the CGI language itself to do it?

Well I want to bring in a small script into a different cgi program. So I was looking at a easy way of doing this without writing more code. I am not that experienced at coding...

moltar

3:01 pm on Aug 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jamesa, the setting does not apply to cgi scripts. At least not to Apache/1.3.27

I know remotely why it could not be done. I think it has something to do with CGI-Server-SSI model. SSI gets parsed on a different level than CGI is ran on, or something like that...

Damian

4:42 pm on Aug 5, 2003 (gmt 0)

10+ Year Member



One way to include a file (containing a navigation menu for example) into a script:

[perl]
# fill in full local path to file
my $navigation = &SSI_INCLUDE("/home/html/navigation.html");

sub SSI_INCLUDE {

my ($file) = @_;
undef $/;
open(FILE, $file);
flock(FILE, 2);
my $content = <FILE>;
flock(FILE, 8);
close($file);
$/ = "\n";
return $content;
}
[/perl]

You would put $navigation in your cgi script's output wherever you want the navigation menu to appear. I don't know why there might be any problems with including files in scripts .. I use the above often and specifically for things like navigation menu's ... but use at your own risk.

moltar

5:26 pm on Aug 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Damian, notice, he wants to include the result of another cgi script, not just a static HTML. This complicates this matter a little.

P.S. Did you color all this code yourself, or is this a new feature of the forum? :)

Damian

5:52 pm on Aug 5, 2003 (gmt 0)

10+ Year Member



Whoops thanks Moltar, I didn't read it properly I guess... that snippet won't work for this problem.

Maybe you can change the two scripts to share files with the relevant subroutines so that you can use the output of one sub routine in multiple scripts.

Luckily I can still contribute something really useful :) :

You get the nice colors by typing
[ perl ] and [ /perl ]

(without the spaces) around your perl fragment.