develooper Front page | perl.beginners | Postings from August 2009

How to pass info to a CGI program without using a form?

Thread Next
From:
boll
Date:
August 23, 2009 09:49
Subject:
How to pass info to a CGI program without using a form?
Message ID:
4A9172AE.1060808@sonic.net
There is a perl program running on my server  that delivers random 
images to a web page.
The image on the html page is specified by this html code:

     <img src="http://localhost/cgi/random_image.cgi"/>

I would like to use this program in several places to display images 
from other directories,
so that one URL might display a random image from the 'vegetables' 
directory,
and a different URL would call the same program but display an image 
from the 'fruits' directory.

Here's where I'm requesting help-
I don't understand how to pass the name of the image directory in the URL,
or how to modify the program to read the directory name.

Here is the program, written to call images from the 'specials' directory:

#! /usr/bin/perl -wT
#
# $Id: rand_image.pl,v 1.11 2002/10/21 19:37:36 nickjc Exp $
#Modified from http://nms-cgi.sourceforge.net

use strict;
use CGI qw(redirect header);
use Fcntl qw(:DEFAULT :flock);
use vars qw($DEBUGGING $done_headers);

# Configuration

# ==================== DEBUGGING: ===================
BEGIN
{
$DEBUGGING = 1;
}

my $use_redirect = 1; #Use redirect and baseurl on server & localhost 
version.
my $baseurl = 'http://localhost/specials/';
my $basedir = '/my-domain.com/specials/';

# =========== image files here: ====================

open LIST, "specials_img_list.txt" #must exist in cgi directory
or die "Image List filehandle not open: $!\n";
my @files = <LIST>; #images (thumbs).

# =============== LOGGING: ==================
my $uselog = 1; # 1 = YES; 0 = NO
my $logfile = 'randimages.log';

# ================ End configuration.

my %content_types = (
jpg => 'image/jpeg',
gif => 'image/gif',
png => 'image/png'
);


# =================== ERRORS: ==================

BEGIN
{
sub fatalsToBrowser
{
my ( $message ) = @_;

if ( $DEBUGGING )
{
$message =~ s/</&lt;/g;
$message =~ s/>/&gt;/g;
}
else
{
$message = '';
}

my ( $pack, $file, $line, $sub ) = caller(0);
my ($id ) = $file =~ m%([^/]+)$%;

return undef if $file =~ /^\(eval/;

print "Content-Type: text/html\n\n" unless $done_headers;

print <<EOERR;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Error</title>
</head>
<body>
<h1>Application Error</h1>
<p>
An error has occurred in the program
</p>
<p>
$message
</p>
</body>
</html>
EOERR
die @_;
};

$SIG{__DIE__} = \&fatalsToBrowser;
}


# =============== IMAGE FILE NAME: =======================

my $pic = '';
while ($pic !~ m/\w+/){
$pic = $files[rand @files];
chomp $pic;
}


# Log Image
if ($uselog) {
sysopen (LOG, $logfile, O_RDWR|O_APPEND|O_CREAT)
or die "Can NOT open logfile: $!\n";
flock(LOG, LOCK_EX)
or die "Can't lock logfile: $!\n";
#print LOG "LOG FILE OPENED \n";
#print LOG "$baseurl$pic\n";
}

if ( $use_redirect )
{
if ( $baseurl !~ m%/$% )
{
$baseurl .= '/';
}
print LOG qq[specials: redirect("$baseurl$pic")\n];
print redirect("$baseurl$pic");
$done_headers++;
}
else
{
if ( $basedir !~ m%/$% )
{
$basedir .= '/';
}

my ( $extension ) = $pic =~ /\.(\S+)$/;

my $ctype = $content_types{$extension} || 'image/png';

open INFILE, "<$basedir$pic" or die "Can't open $basedir$pic - $!";
binmode INFILE;
local $/;

my $image = <INFILE>;
close INFILE;

print header(-type => $ctype),
$image;
print LOG "$baseurl$pic\n";
}
close (LOG)
or die "Can't close logfile: $!\n";


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About