Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Automation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Image Tag Launches CGI Program

Should you need to run a CGI script when a web page loads, an IMG image tag can be utilized. The SRC of the image tag is the URL of the script.

The CGI script runs, doing what it's supposed to do for you.

But before the script quits, it sends the content of an image file to the browser. This satisfies the browser's request.

The image can be a transparent ____.gif or ____.png, a logo, or any other image that web pages can display with an IMG tag.

This article has examples of everything you need to run a CGI program when a page loads by putting the URL of the program into an IMG tag.

Note: The introduction article, "Running a CGI Program On Page Load," contains numerous examples for automatically running a script when a page loads and has links to several different methods of implementing those features for your web site.

Using an IMG Image Tag to Automatically Run a CGI Program When a Web Page Loads

When a web page loads and the browser finds an IMG tag in the source code, the browser makes a request for the image. It makes the request at the SRC URL it finds in the IMG tag.

If the URL is the URL of a CGI script, the script runs.

The CGI script must, however, return an image to the browser. Otherwise, the browser is likely to display a broken image icon on the page.

An example IMG tag and an example CGI script are provided below.

The IMG Tag

Here is an example IMG tag with the URL of a CGI program:

<img src="/cgi-bin/script.cgi" height="11" width="33" alt="test">

The URL of the CGI script may have a question mark at the end followed by information to send to the CGI program for processing. The information following the question mark can be hard coded into the URL or it can be information gathered by JavaScript and inserted into the URL.

We'll use the current web page URL as an example of how JavaScript might accomplish the act of putting information into the URL.

In order to pull it off, the JavaScript must write the entire IMG tag. This allows it to insert anything it wants to insert into the URL.

Here is the example:

<script type="text/javascript" language="JavaScript"><!--
var url = "/cgi-bin/script.cgi?ThisPageURL=" + escape(document.URL);
document.write('<img src="' + url + '" height="11" width="33" alt="test">');
//--></script>

Sending the current web page URL to CGI programs that need the information is more reliable than having the CGI program itself consult the environment variables. Many users now have such strict anti-logging and privacy browser preferences, or strict personal firewall software, that the browser is prevented from providing referrer information.

The CGI Program

Here is a CGI program that returns an image to the browser, suitable as the SRC URL in the above example IMG tags:

#!/usr/bin/perl
use strict;
# script can do other stuff here
#----------#
# Put the image file to be sent to the browser 
#   in the same directory where this script is 
#   installed. Put the image file's name between 
#   the quotes on the next line.
my $image = "image.gif";
open R,"<$image"; 
binmode R;
print "Content-type: image/*\n\n";
binmode STDOUT;
while(<R>) { print $_; }
close R;
# end of script

The part of the script that returns the image to the browser is below the #----------# line.

Verify the image file name.

The script assumes the image is in the same directory the script is installed in. If the image is at some other location on the server, provide the relative or absolute directory path to the script as part of the file name.

The CGI program used in this implementation may not return an HTML web page. It must return only the contents of an image file to the browser. If it returned a web page, and the browser was expecting an image, then the browser will likely display the broken image icon.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC