CGI Answers Ask John Logo
Ask John · Java · Perl · CGI · Xara · Other · John

Make sure that you've checked the answers and the FAQs before you Ask a Question.

Professional consultancy and software development is available through my company Castle Amber.

Since most of my CGI programs are written in Perl most answers are illustrated with a piece of Perl code. See also Perl Answers.

1 General Information

1.1 Which web sites have information on CGI?

2 Redirection

2.1 How can I redirect a user time dependend?

The following Perl CGI script redirects the visitor between 20:00 and 21:00 to happyhour.html and to paymore.html otherwise. According to the documentation (perldoc CGI) it is often necessary to specify the full (absolute) URL due to server optimization.

You can use this script for example when you have a webcam on a server which is only available a few hours a day.

#!/usr/local/bin/perl -wT
#
# happyhour.cgi - redirects visitor time depended
# 0.01 (10-Jan-1999)
# (c) Castle Amber, 1999

use strict;
use CGI qw(:cgi);

my @time = localtime;

my $hour = $time[2];

my $location;

if ($hour >= 20 && $hour <= 21)
{
    $location = "http://www.yoursite.com/happyhour.html";
}
else
{
    $location = "http://www.yoursite.com/paymore.html";
}

print redirect($location);
This script uses the time on the server (which in most cases is exactly what you want). So always check this with date because the server can reside in a different time zone.

For more information see Programming Perl page 185-186 (localtime), Perl Cookbook page 688-690 and perldoc CGI.

2.2 How can I redirect to a random URL?

This small Perl CGI script redirects the visitor to an URL which is selected at random from a list of URLs. Enter each URL on a new line between the opening and closing bracket of the qw operator.
#!/usr/local/bin/perl -wT
#
# random.cgi - redirects user to a random URL
# 0.01 (19-Jan-1998)
# (c) Castle Amber, 1999

use strict;
use CGI qw(:cgi);

my @URLS = qw(

	http://www.castleamber.com/
	http://www.perl.com/
	http://www.javasoft.com/
);

srand(time() ^ ($$ + ($$ << 15)));
print redirect($URLS[rand @URLS]);
You can also use this script to return a random image. Use URLs of images and the following HTML code:
<IMG SRC="/cgi-bin/random.cgi" ALT="Random Picture">
Don't forget to add WIDTH and HEIGHT if all images have the same width and the same height. This way the page renders much faster.

For more information see section 2.1, Programming Perl page 223 (srand), Perl Cookbook page 688-690 and perldoc CGI.

3 Logging

3.1 How do I keep track of the number of downloads?

An easy way to keep track of the number of downloads is to let the visitor enter some information and append the information to a log file. This is exactly what the following script does.

For an example of a form to call this script see the Castle Amber AmberCam page.

#!/usr/local/bin/perl -wT
#
# dltrack.cgi - keeps track of downloads
# 0.01 (27-Jan-1998)
# (c) Castle Amber, 1999

use strict;
use Fcntl qw(:DEFAULT :flock);
use CGI;

$| = 1;

my $LOG = "/path/to/your/home/downloadlog.txt";

my $query = new CGI;

#--- try
eval
{
    #--- open the file and create if it doesn't exist
    sysopen(FILE, $LOG, O_WRONLY | O_CREAT, 0644)
	or die "can't open/create log: $!";
    #--- lock the file (exclusive, for writing)
    flock(FILE, LOCK_EX)
	or die "can't lock file: $!";
    #--- move to end of file
    seek(FILE, 0, 2)
	or die "seek failed: $!";
    
    #--- write out data
    print FILE	"name:\t",  $query->param('name'),  "\n", 
		"email:\t", $query->param('email'), "\n",  
		"--\n"
	or die "print failed: $!";
    close(FILE)
	or die "close failed: $!";
};
#--- catch
unless ($@)
{
    print $query->redirect('http://www.yoursite.com/file.zip');
}
else
{
    print $query->header(-TYPE => 'text/plain'), $@;
}
The script uses file locking to make sure that log lines are not interleaved when multiple processes write to the same file.

You can add more fields besides name and email by adding additional calls to the param function.

Notice that this script uses eval to simulate a try and catch construct. When an error occurs the message is returned to the browser as plain text. The message reports also the line in the script that caused the error. If you don't want this add an \n to the end of each die message.

If no error occured the visitor is redirected to the file to be downloaded.

See also 2 Redirection, Perl Cookbook page 245-247 (Locking a file).


Ask John · Java · Perl · CGI · Xara · Other · John

© Copyright 1999 by John Bokma