Advanced Rotating Banner with Links and Alt Text
SSI Random Image Displayer is an extended version of the basic Random Image Displayer. This version outputs a complete IMG tag with corresponding random link, alt text, width, height, and other configurable attributes using Server Side Includes.
SSI Random Image Displayer extends the basic random image script by outputting a complete HTML snippet rather than just image data. This allows each random image to have its own associated link, alt text, width, height, and other attributes.
The script is called via SSI directive in your .shtml page. It randomly selects an entry from its configuration and outputs the complete HTML (image tag wrapped in anchor tag if a link is specified). The browser receives ready-to-render HTML as part of the page.
| File | Description |
|---|---|
ssi_rand_image.pl |
Main Perl script that outputs random image HTML tags |
README |
Installation instructions and configuration guide |
Each image can have its own destination URL. Perfect for clickable banner ads with tracking.
Specify alt text for each image for better accessibility and SEO.
Set width and height attributes to prevent layout shift during image loading.
Open links in new windows or specific frames using the target attribute.
Each page load selects a truly random image from your configured set.
Simple data structure to configure multiple images with all their attributes.
| Option | Description | Example |
|---|---|---|
image |
URL path to the image file | /images/banners/sponsor1.gif |
link |
Destination URL when image is clicked | https://sponsor.com/ |
alt |
Alternative text for accessibility | Visit Our Sponsor |
width |
Image width in pixels | 468 |
height |
Image height in pixels | 60 |
border |
Border width (0 for no border) | 0 |
target |
Link target window/frame | _blank |
Options +Includes and AddHandler server-parsed .shtml.
ssi_rand_image.pl to your cgi-bin directory.
#!/usr/bin/perl).
chmod 755 ssi_rand_image.pl
<!--#exec cgi="/cgi-bin/ssi_rand_image.pl"--> to your .shtml page.
<!-- Basic SSI include -->
<!--#exec cgi="/cgi-bin/ssi_rand_image.pl"-->
<!-- In context of a page -->
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome to My Site</h1>
<!-- Random banner will appear here -->
<div class="banner-container">
<!--#exec cgi="/cgi-bin/ssi_rand_image.pl"-->
</div>
<p>Main content goes here...</p>
<!-- Another random banner in footer -->
<footer>
<!--#exec cgi="/cgi-bin/ssi_rand_image.pl?set=footer"-->
</footer>
</body>
</html>
<!-- Example output from script -->
<a href="https://sponsor.com/" target="_blank">
<img src="/images/banners/sponsor1.gif"
alt="Visit Our Sponsor"
width="468" height="60" border="0">
</a>
#!/usr/bin/perl
use strict;
use warnings;
# Configuration - array of image data
my @images = (
{
image => '/images/banners/sponsor1.gif',
link => 'https://sponsor1.com/',
alt => 'Sponsor 1 - Premium Hosting',
width => 468,
height => 60,
target => '_blank',
},
{
image => '/images/banners/sponsor2.gif',
link => 'https://sponsor2.com/',
alt => 'Sponsor 2 - Web Design',
width => 468,
height => 60,
target => '_blank',
},
{
image => '/images/banners/sponsor3.png',
link => 'https://sponsor3.com/',
alt => 'Sponsor 3 - Domain Names',
width => 468,
height => 60,
target => '_blank',
},
);
# Select random image
my $img = $images[int(rand(@images))];
# Build HTML output
my $html = '';
# Start with link if present
if ($img->{link}) {
$html .= qq{{target};
$html .= qq{ rel="noopener noreferrer"} if $img->{target} eq '_blank';
$html .= '>';
}
# Add image tag
$html .= qq{
{alt};
$html .= qq{ width="$img->{width}"} if $img->{width};
$html .= qq{ height="$img->{height}"} if $img->{height};
$html .= qq{ border="0"};
$html .= '>';
# Close link if present
if ($img->{link}) {
$html .= '';
}
# Output
print "Content-type: text/html\n\n";
print $html;
exit 0;
<?php
/**
* SSI Random Image Displayer - PHP Version
* Can be included directly in PHP pages
*/
$images = [
[
'image' => '/images/banners/sponsor1.gif',
'link' => 'https://sponsor1.com/',
'alt' => 'Sponsor 1 - Premium Hosting',
'width' => 468,
'height' => 60,
'target' => '_blank',
],
[
'image' => '/images/banners/sponsor2.gif',
'link' => 'https://sponsor2.com/',
'alt' => 'Sponsor 2 - Web Design',
'width' => 468,
'height' => 60,
'target' => '_blank',
],
[
'image' => '/images/banners/sponsor3.png',
'link' => 'https://sponsor3.com/',
'alt' => 'Sponsor 3 - Domain Names',
'width' => 468,
'height' => 60,
'target' => '_blank',
],
];
// Select random image
$img = $images[array_rand($images)];
// Build HTML
function buildRandomImage($img) {
$html = '';
// Start link if present
if (!empty($img['link'])) {
$html .= '
/**
* SSI Random Image Displayer - JavaScript Version
* Full-featured client-side banner rotation
*/
class BannerRotator {
constructor(element, banners, options = {}) {
this.element = typeof element === 'string'
? document.querySelector(element)
: element;
this.banners = banners;
this.options = {
interval: 0, // 0 = no auto-rotation
fadeTransition: true,
transitionDuration: 300,
trackClicks: false,
trackImpressions: false,
...options
};
this.currentIndex = -1;
this.init();
}
init() {
this.showRandom();
if (this.options.interval > 0) {
setInterval(() => this.showRandom(), this.options.interval);
}
}
showRandom() {
// Avoid showing same banner twice in a row
let newIndex;
do {
newIndex = Math.floor(Math.random() * this.banners.length);
} while (newIndex === this.currentIndex && this.banners.length > 1);
this.currentIndex = newIndex;
const banner = this.banners[newIndex];
if (this.options.fadeTransition) {
this.fadeTransition(banner);
} else {
this.renderBanner(banner);
}
if (this.options.trackImpressions) {
this.trackImpression(banner);
}
}
fadeTransition(banner) {
this.element.style.transition = `opacity ${this.options.transitionDuration}ms`;
this.element.style.opacity = '0';
setTimeout(() => {
this.renderBanner(banner);
this.element.style.opacity = '1';
}, this.options.transitionDuration);
}
renderBanner(banner) {
let html = '';
// Start link if present
if (banner.link) {
const target = banner.target ? ` target="${banner.target}"` : '';
const rel = banner.target === '_blank' ? ' rel="noopener noreferrer"' : '';
const clickHandler = this.options.trackClicks
? ` onclick="return bannerClick('${banner.id || banner.image}')"` : '';
html += ``;
}
// Image tag
html += `
';
// Close link
if (banner.link) {
html += '';
}
this.element.innerHTML = html;
}
trackImpression(banner) {
// Implement your tracking logic
console.log('Impression:', banner.id || banner.image);
// Example: send to analytics
// navigator.sendBeacon('/track/impression', JSON.stringify({ banner: banner.id }));
}
}
// Usage examples:
const banners = [
{
id: 'sponsor1',
image: '/images/banners/sponsor1.gif',
link: 'https://sponsor1.com/',
alt: 'Sponsor 1 - Premium Hosting',
width: 468,
height: 60,
target: '_blank'
},
{
id: 'sponsor2',
image: '/images/banners/sponsor2.gif',
link: 'https://sponsor2.com/',
alt: 'Sponsor 2 - Web Design',
width: 468,
height: 60,
target: '_blank'
},
{
id: 'sponsor3',
image: '/images/banners/sponsor3.png',
link: 'https://sponsor3.com/',
alt: 'Sponsor 3 - Domain Names',
width: 468,
height: 60,
target: '_blank'
}
];
// Basic usage
new BannerRotator('#banner-container', banners);
// With auto-rotation
new BannerRotator('#rotating-banner', banners, {
interval: 10000, // 10 seconds
fadeTransition: true
});
// With tracking
new BannerRotator('#tracked-banner', banners, {
trackClicks: true,
trackImpressions: true
});
// Click tracking function (called from onclick)
function bannerClick(bannerId) {
console.log('Click:', bannerId);
// navigator.sendBeacon('/track/click', JSON.stringify({ banner: bannerId }));
return true; // Allow link to proceed
}
Main Perl script that outputs random image HTML
Installation instructions and configuration guide
<!--#command parameter="value"-->. SSI allows you to include dynamic content in otherwise static HTML pages without a full server-side language like PHP. The #exec cgi directive runs a CGI script and includes its output.
Options +Includes to enable SSI, and AddHandler server-parsed .shtml to tell Apache which files to process. You can also use AddOutputFilter INCLUDES .html to process all HTML files. Ensure mod_include is enabled: a2enmod include on Debian-based systems.
<!--#exec cgi="/cgi-bin/ssi_rand_image.pl?set=sponsors"-->. The script reads $ENV{QUERY_STRING} and loads the appropriate image configuration based on the set parameter. You can store different sets in separate config files or in a hash within the script.