For example, if it picks 5678.gif from the directory, it could point to hyperlink [domain.com...] and if it picks 2369.gif it points to [domain.com...]
Can anyone recommend one?
If its any help I went for javascript that can just be placed where you want the images to appear and a call to a perl script that takes care of reading various images with their URLs, dumps them in an array and then selects one at random.
So far got the admin page for adding/deleting images. Next steps will hopefully be the ability to weight certain images and also target images based on something like title tag of page.
Almost forgot - added some click logging too!
I wouldn't exactly say its production code (far from it!)but if you wanna take a look visit the site in profile - the 125x125 down left uses the script. Look at the source and you can get the javascript. Heres the main perl script - (no laughing now!):
Also, its a while since I worked on this and you'll probably find that set up values are hardcoded into the script rather than declared in the setup bit and also some varibales declared but not used.
#!/usr/bin/perl
####################################################################
####################################################################
## Banner rotation manager. Call the script via a javascript tag ##
## this inserts banners with link url into page. ##
## ##
####################################################################
####################################################################
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser carpout);
####################################################################
####################################################################
## ##
## Set up variables below ##
## ##
####################################################################
####################################################################
# set $domain to your domain name
$domain = $ENV{"SERVER_NAME"};
# set path to path where your admin form resider. Leave as "" if its is in root
$filePath = "http://www.xyz.com/cgi-bin";
# set file name of admin form
$fileName = "banner.pl";
###### End of set up ######
&load_vars;
&determine_action;
&write_file;
&delete_images;
&print_header;
&display;
exit;
sub load_vars {
# Get all of the data provided by the HTML form
$image = param("image");
$link = param("link");
$width = param("width");
$height = param("height");
@deletions = param("banner_del");
$query = $ENV{"QUERY_STRING"};
}
### Determine suitable action for the script
### Presence of query string implies a banner is clicked on
### Anything else should be banner admin
sub determine_action {
if ($query) {
&click_log;
&redirect;
exit;
}
}
### Add images and link URLs to the data file
sub write_file {
$entry = "$image¬¬$link¬¬$width¬¬$height";
open (FILE, "banner_db.txt");
lockfile (FILE);
@banner_info = <FILE>;
unlockfile (FILE);
close FILE;
chomp @banner_info; #delete all returns from the scalar
#build a temp hash
foreach $banner (@banner_info){
$bannerHash{$banner}++;
}
#test if your key exists and write it if it doesnt.
unless (exists $bannerHash{$entry} ¦¦ $entry eq "¬¬¬¬¬¬") {
open(FILE, ">>banner_db.txt");
lockfile (FILE);
print FILE "$image¬¬$link¬¬$width¬¬$height\n";
unlockfile (FILE);
close FILE;
}
}
### Print the html header
sub print_header {
print "Content-type: text/html\n\n";
}
### Delete images selected by user from data file
sub delete_images {
open (FILE, "banner_db.txt");
lockfile (FILE);
@banner_info = <FILE>;
unlockfile (FILE);
close FILE;
chomp @banner_info; #delete all returns from the scalar
chomp @deletions;
foreach $banner (@banner_info){
$bannerHash{$banner}++;
}
if (@deletions) {
print "Content-type: text/html\n\n";
foreach $deletions (@deletions) {
foreach $banner (keys %bannerHash) {
if ($banner eq $deletions) {
delete($bannerHash{"$banner"});
}
}
}
}
open(FILE, ">banners.txt");
lockfile (FILE);
foreach $banner (keys %bannerHash){
print FILE "$banner\n";
}
unlockfile (FILE);
close FILE;
}
sub display {
print <<HTML;
<p><b>Add to database</b></p>
<form method="post" action="/cgi-bin/banner.pl">
<table width="100%" border="0" summary="">
<tr>
<td>Link URL:</td>
<td>
<input type="text" name="link" size="40" maxlength="256">
</td>
</tr>
<tr>
<td>Image Url:</td>
<td>
<input type="text" name="image" size="40" maxlength="256">
</td>
</tr>
<tr>
<td>Image Width:</td>
<td>
<input type="text" name="width" size="40" maxlength="256">
</td>
</tr>
<tr>
<td>Image Height:</td>
<td>
<input type="text" name="height" size="40" maxlength="256">
</td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
HTML
open (FILE, "banner_db.txt");
lockfile (FILE);
@banner_info = <FILE>;
unlockfile (FILE);
close FILE;
chomp @banner_info;
$numBanners = @banner_info;
print "<p>There are $numBanners banners in the database<\/p>\n";
print <<HTML;
<form method="post" action="banner.pl">
<table width="100%" border="1" summary="">
<tr>
<td>Delete Banner</td>
<td>Example</td>
<td>Image URL</td>
<td>Target URL</td>
<td>Width</td>
<td>Height</td>
</tr>
HTML
foreach $banner (@banner_info){
($imageURL, $linkURL, $width, $height) = split /¬¬/ , $banner;
print <<HTML;
<tr>
<td><input type="checkbox" name="banner_del" value="$imageURL¬¬$linkURL¬¬$width¬¬$height">
<td><a href="$linkURL"><img src="$imageURL"></a></td>
<td><font size="1">$imageURL</font></td>
<td><font size="1">$linkURL</font></td>
<td><font size="1">$width</font></td>
<td><font size="1">$height</font></td>
</tr>
HTML
}
print <<HTML;
</table>
<input type="submit" value="Submit">
</form>
HTML
}
sub click_log {
my $destination = $query;
my $time = gmtime(time);
my $ip = $ENV{"REMOTE_ADDR"};
my $referer = $ENV{"HTTP_REFERER"};
open (FILE, ">>clicks.txt");
lockfile (FILE);
print FILE "$time%%$destination%%$ip%%$referer\n";
unlockfile (FILE);
close FILE;
}
sub redirect {
my $destination = $query;
$co = new CGI;
print $co->redirect($destination);
}
sub lockfile
{
my $count = 0;
my $handle = shift;
until (flock($handle, 2)) {
sleep .10;
if(++$count > 50) {
print "Server Busy";
exit;
}
}
}
sub unlockfile
{
my $handle = shift;
flock($handle, 8);
}
***************************************
***************************************
***************************************
Heres the perl script that displays the banner - again, probably need to weed out a load of hard coded variables:
#!/usr/bin/perl
# set path to path where your admin form resides. Leave as "" if its is in root
$filePath = "http://www.domain.co.uk/cgi-bin";
# set file name of admin form
$fileName = "banner.pl";
open (FILE, "banner_db.txt");
&lockfile (FILE);
@banner_info = <FILE>;
&unlockfile (FILE);
close FILE;
chomp @banner_info; #remove line ends
$countBanners = @banner_info;
$random = int rand ($countBanners) + 0;
$banner = @banner_info[$random];
($imageURL, $linkURL, $width, $height) = split /¬¬/ , $banner;
$linkURL = $filePath ."\/" . $fileName . "\?" . $linkURL;
print "Content-type: application/x-javascript\n\n";
print qq[link = '$linkURL'\n];
print qq[image = '$imageURL'\n];
print qq[width = '$width'\n];
print qq[height = '$height'\n];
exit;
sub lockfile
{
my $count = 0;
my $handle = shift;
until (flock($handle, 2)) {
sleep .10;
if(++$count > 50) {
print "Content-type: application/x-javascript\n\n";
print qq[link = 'http://www.domain.co.uk'\n];
print qq[image = '/images/125x125.gif'\n];
print qq[width = '125'\n];
print qq[height = '125'\n];
exit;
}
}
}
sub unlockfile
{
my $handle = shift;
flock($handle, 8);
}
Hope you can pick your way through that OK!
[edited by: jatar_k at 5:29 pm (utc) on Nov. 22, 2002]
[edit reason] generalized domains [/edit]