#!/usr/local/bin/perl print "Content-type: text/html\n\n"; ##################### DISCLAIMER ######################### ## I make no express guarantees...hey, it works for me. ## ## It should work for you. It's quick, easy, and easily ## ## portable to lots of pages, simply by renaming the cgi ## ## script to another name and running another set of ## ## banners on another page, under that filename ## ##################### COPYRIGHT ########################## ## clicktrade.cgi Version 1.0, written by Eric Beck, ## ## Copyright, 1999 All rights reserved (Whoopee) ## ## Use this program as you see fit, per the GNU license. ## #################### INSTRUCTIONS ########################## ## Very simple script to do a banner rotation. Used ## ## as a Server Side Include ## ## Simply edit the location of the counter file, set ## ## the number of banners you want to use. ## #################### DEFINE VARIABLES ###################### ## ## ## ## ## $cfile - The COUNTER file to increment the rotation. ## ## Change this to the location of your count file. This ## ## should be in a secure location out of the public html ## ## path ideally, although it doesn't have to be. HEY, ## ## YOU'VE GOT TO MAKE THIS FILE, AND UPLOAD IT FIRST. Put ## ## it where you like and call it what you like; just def- ## ## ine it below here. If you do change it's variable ## ## name though to something other than $cfile, you'll ## ## have to change all instances of '$cfile' in the lock ## ## and unlock routines, and every other instance of $cfile## ############################################################ # Complete path to counter file. $cfile = "/big/dom/xwebdesi/cgi-bin/clicktrade_count.txt"; ############################################################ ## Next: ## ## Change this to the number of banners in your rotation ## ## Note: If you change this figure from '10', then you ## ## will have to add IF statements onto the end of the ## ## script. See ### NOTE ### ## ############################################################ $number_of_banners = 10; ############################################################ ## Define your banner code for each banner. ## ## Format: $b[number] = "[exact banner code goes between ## ## the quotes]"; ## ## ## ## See the EXAMPLE in Banner code for if ($unique == 1) ## ## and you'll see how to modify this code if you wanted ## ## to put all the gifs in one spot. I started to do this ## ## and then thought, what the hell, I'm not going to get ## ## that sophisticated as to start building a whole ## ## complicated configurable program; I just wanted ## ## something to rotate a bunch of banners using SSI. If ## ## I can't scroll down the page and paste the banner code ## ## in where it goes, then I need to get a life or stop ## ## drinking coffee. Maybe one day when I've got time, ## ## work on suping it upt to something more sophisticated. ## ############################################################ ########################### MAIN PROGRAM ################### ## Get the present count if (-w $cfile) { &get_file_lock ("$cfile.lock"); ## We want to lock this of course. open (CFILE,"$cfile"); while () { chomp; $unique = int($_); } close CFILE; ## Increase the count number by one. $unique ++; ## Reduce the count by 10 ## We're using 10 banners in our rotation, so when the ## count gets to 11, we send it back to 1 if ($unique == ($number_of_banners + 1)) { $unique = ($unique - $number_of_banners); } open (CFILE,">$cfile"); print CFILE "$unique"; close CFILE; &release_file_lock ("$cfile.lock"); } ############################################################ ## Edit these next ten Banner Code URLs to your own. ## ## They rotate in order, 1 through 10. ## ## The code just goes between the print < end_html } if ($unique == 2) { print < end_html } if ($unique == 3) { print < end_html } if ($unique == 4) { print < end_html } if ($unique == 5) { print < end_html } if ($unique == 6) { print < end_html } if ($unique == 7) { print < end_html } if ($unique == 8) { print < end_html } if ($unique == 9) { print < end_html } if ($unique == 10) { print < end_html } ############################################################ ## Put more banner definitions below here, after the 10th ## ############################################################ ######################### NOTE # ########################### ## Add more if statements here if going over 10 banners ## ## in the rotation. Just change the ($unique == 10) to ## ## ($unique == 11), ($unique == 12) and so on. The first ## ## one is done below for you so you just UNCOMMENT ## ## it to go for it, AND CHANGE THE NUMBER AT THE TOP TO ## ## 11 BANNERS, INSTEAD OF 10. Reapeat at will. ## ############################################################ #if ($unique == 11) { # print < #end_html #} ##################### END MAIN PROGRAM #################### ##################### SUB ROUTINES ######################## ## THIS PARSE FORM SUB GOES IN ALL CGI SCRIPTS IN ## ## CASE WE'RE MAKING A SCRIPT THAT IS TAKING FORM VARs ## ## PARSE THE FORM FOR DATA ## ########################################################### sub parse_form { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); if (length($buffer) < 5) { $buffer = $ENV{QUERY_STRING}; } @pairs=split(/&/,$buffer); foreach $pair(@pairs) { ($name, $value)=split(/=/,$pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][A-F0-9])/pack("C",hex($1))/eg; if($input{$name} eq "") { $FORM{$name} = $value; push (@FIELDS,$name); } else { $FORM{$name} = $FORM{$name}." ".$value; } } } ####################################################################### # get_file_lock # ####################################################################### # get_file_lock is a subroutine used to create a lockfile. # Lockfiles are used to make sure that no more than one # instance of the script can modify a file at one time. A # lock file is vital to the integrity of your data. # Imagine what would happen if two or three people # were using the same script to modify a shared file (like # the error log) and each accessed the file at the same # time. At best, the data entered by some of the users # would be lost. Worse, the conflicting demands could # possibly result in the corruption of the file. # # Thus, it is crucial to provide a way to monitor and # control access to the file. This is the goal of the # lock file routines. When an instance of this script # tries to access a shared file, it must first check for # the existence of a lock file by using the file lock # checks in get_file_lock. # # If get_file_lock determines that there is an existing # lock file, it instructs the instance that called it to # wait until the lock file disappears. The script then # waits and checks back after some time interval. If the # lock file still remains, it continues to wait until some # point at which the admin has given it permissios to just # overwrite the file because some other error must have # occurred. # # If, on the other hand, the lock file has dissappeared, # the script asks get_file_lock to create a new lock file # and then goes ahead and edits the file. # # The subroutine takes one argumnet, the name to use for # the lock file and is called with the following syntax: # # &get_file_lock("file.name"); sub get_file_lock { local ($lock_file) = @_; local ($endtime); $endtime = 20; $endtime = time + $endtime; # We set endtime to wait 20 seconds. If the lockfile has # not been removed by then, there must be some other # problem with the file system. Perhaps an instance of # the script crashed and never could delete the lock file. while (-e $lock_file && time < $endtime) { sleep(1); } # open(LOCK_FILE, ">$lock_file") || &file_open_error ("$lock_file", "Lock File Routine", __FILE__, __LINE__); # Note: If flock is available on your system, feel free to # use it. flock is an even safer method of locking your # file because it locks it at the system level. The above # routine is "pretty good" and it will server for most # systems. But if youare lucky enough to have a server # with flock routines built in, go ahead and uncomment # the next line and comment the one above. flock(LOCK_FILE, 2); # 2 exclusively locks the file } ####################################################################### # release_file_lock # ####################################################################### # release_file_lock is the partner of get_file_lock. When # an instance of this script is done using the file it # needs to manipulate, it calls release_file_lock to # delete the lock file that it put in place so that other # instances of the script can get to the shared file. It # takes one argument, the name of the lock file, and is # called with the following syntax: # # &release_file_lock("file.name"); sub release_file_lock { local ($lock_file) = @_; flock(LOCK_FILE, 8); # 8 unlocks the file # As we mentioned in the discussion of get_file_lock, # flock is a superior file locking system. If your system # has it, go ahead and use it instead of the hand rolled # version here. Uncomment the above line and comment the # two that follow. # close(LOCK_FILE); # unlink($lock_file); }