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


In reply to Re: Calling a subroutine and dispalying an image using ssi by tachyon
in thread Calling a subroutine and dispalying an image using ssi by ginocapelli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.