in reply to Calling a subroutine and dispalying an image using ssi

This is really two questions. First how do I use server side includes to execute a script and second how do I write a cgi script to select/display a random image within the constraints enforced by SSI.

To use a SSI to execute a CGI script you just embed a link like this in your HTML:

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

Of course support for SSI will need to be enabled on the server for this to work, but provided SSI is enabled for this HTML doc the output of the script 'myscript.cgi' will be embedded into the HTML.

The output of this CGI script may only be text. It can not be a graphic image or other binary data. Thus we can't output the image directly but fortunately we dont need to. All we need to do is output a complete image tag that points to the desired image. This image tag will get included by the server and thus the image will get displayed by the browser.

Here is some code for a very simple script to rotate five banners:

#!/usr/bin/perl -w use strict; my @banner = ( '<img src="/image1.gif" border=0 width=500 height=100>', '<img src="/image1.gif" border=0 width=500 height=100>', '<img src="/image1.gif" border=0 width=500 height=100>', '<img src="/image1.gif" border=0 width=500 height=100>', '<img src="/image1.gif" border=0 width=500 height=100>'); # seed random generator and select an image from array srand (time()^($$+($$<<15))); my $image = int rand @banner; # print our header and random image link for SSI print "Content-type: text/html\n\n"; print $banner[$image]; exit;

You could of course generate the image tags dynamically by reading a directory but hard coding them is much faster and as you will call this banner script for every page you want it to be fast. I have not included the A HREF link tags you will want for brevity.

Hope this helps

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Calling a subroutine and dispalying an image using ssi
by Anonymous Monk on Jul 07, 2001 at 23:43 UTC
    I think you got what I was trying to say... I have a program uses the banner_rotator subroutine and displays the banner in a header.txt file as $banner. I am using a banner rotater right now that uses img tags OR ssi to display a random banner. I use this on all of my .htm files outside of my cgi-bin. I wan't to use the banner_rotator from my whatever.pl file so that I don't have to update two scripts when I add a new banner.
      You are asking how you can put the code in one file, then reference it from multiple ssi scripts? Look up require. Hard-code the full absolute path to begin with, since relative paths in cgi are another challange.

      —John