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!

Non-Display Image Silently Causes CGI Program to Run

JavaScript can be used to create an image object (an area in computer memory that holds the details the browser will need to display an image). No HTML code is needed.

CGI programs can be launched with JavaScript by telling the browser to fill the image object with information, and giving the browser the URL of the CGI program as the source for the image information it is to use.

This is an exciting method used by savvy programmers to run CGI programs in the background.

The program can run when a page loads or when certain other events happen, such as which link was clicked. The program can run more than once, as often as needed, in fact.

Some implementation ideas:

  1. The program can log details of certain activities, like which link was clicked, to continue using the previous example.

    The length of time the user spent on the page can be logged. Right-clicks can be logged, along with information about where the cursor was when the mouse was right-clicked.

    Numerous activities can be detected and logged.

  2. The program can set and read cookies — which JavaScript can do by itself but, unlike JavaScript, a CGI program can upate databases.

  3. The programs can update databases with items of information like browser used, IP address, and prior visit history (if cookies are used).

Before the CGI script ends its run, it sends the content of an image file to the browser. This satisfies the browser's request.

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.

Page Events Automatically Run a CGI Program

To cause a page event to automatically run a CGI program, JavaScript is used to detect the event and then launch the program.

The following JavaScript detects a page load event (when a page completes loading) and sends the URL of itself to the CGI program.

<script type="text/javascript" language="JavaScript">
<!-- Copyright 2005 Bontrager Connection, LLC
function LaunchCGIprogram() {
var holder = new Image();
holder.src = "/cgi-bin/logger.cgi?" + document.location.href;
}
window.onload = LaunchCGIprogram;
//-->
</script>

The above JavaScript can be placed in the HEAD or the BODY area of the web page source code.

Before putting the page with the JavaScript on your server, verify the URL of the CGI program is correctly specified between the quotes on the line beginning with holder.src (the question mark must be at the end of the URL).

The browser expects an image to be returned. Therefore, the CGI program should, but doesn't actually need to, return an image to the browser. But it does need to return something, even if it's a web page.

The reason the CGI program doesn't need to return an image is because the image will never actually be displayed. The information is only stored in memory; the browser is never told to display it. The CGI program needs to return something, however, so the browser doesn't just sit there waiting for the server to respond until it times out.

The CGI Program

Following is a CGI program that logs what was sent to it with the JavaScript and includes the site visitor's IP address. The log file name is log.txt — which may be changed. After updating the log file, an image is returned to the browser.

#!/usr/bin/perl
use strict;
open W,">log.txt" unless open W,">>log.txt";
print W "$ENV{QUERY_STRING}\t$ENV{REMOTE_ADDR}\n";
close W;
# 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

Before installing the above CGI program, verify the image file name, changing the file name if needed.

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 does not return an HTML web page. It returns only the contents of an image file to the browser.

Email Notification When a Page Event Occurs

Email notification and many other features are available if the CGI program being used is Master Form V4.

To use Master Form V4, the JavaScript needs to provide the email template file and the URL of the image to be returned to the brower.

Here is the previous JavaScript modified to do exactly that.

<script type="text/javascript" language="JavaScript">
<!-- Copyright 2005 Bontrager Connection, LLC
function LaunchCGIprogram() {
var parameters = "URL=" + document.location.href;
parameters += "&emailtemplate=" + "notifyemail.txt";
parameters += "&flowto=" + "http://example.com/image.gif";
var holder = new Image();
holder.src = "/cgi-bin/MasterFormV4.cgi?" + parameters;
}
window.onload = LaunchCGIprogram;
//-->
</script>

The location of the image is specified as a complete http://... URL, between the quotes on the line containing "&emailtemplate=" — if the email template file is not in the directory where the MasterFormV4.cgi script is located, the directory path must be provided with the file name, which may be a directory path relative to the CGI script.

The location of the email template file is between the quotes on the line containing "&flowto="

The URL to the MasterFormV4.cgi script is specified between the quotes on the line beginning with holder.src (the question mark at the end of the URL is required).

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