#!/usr/local/bin/perl # # Name :cookiead # Author :Kang SoonLai # Date :Wed Nov 20 1996, 03:26:04 AM v1.0 # Last Mod:Wed Apr 09 1997, 03:37:12 PM v1.1 # Fri Oct 17 18:01:11 CDT 1997 v1.2 # - added statistics for banners # Path :/users/skang/public_html/programs/cookiead # Email :skang@ittc.ukans.edu # WWW :http://www.ittc.ukans.edu/~skang # # This is an ad banner script that randomly displays the banner img and # correctly(hopefully) routes the ref to the correct URL. # ** DOES NOT require server side include! ** # # Usage: # 1. Change the $dir var to where u want the stat to be store at. # 2. Create a rc file named stat. This file stores the urls, the # paths to the banner, freq of appearence. eg: # # url1%%img_file1%%freq_of_appearence # url2%%img_file2%%freq_of_appearence # . # . # # If you plan to use cookiead for several other pages, you can then create # another rc files, and you must append the name of the rc file to the # cgi call. Here are the examples: # # default: # Sponsor # # rc file, stat2, given: # Sponsor # # 3. create a pics dir in the $dir where you have just created. # put all the image files there. eg: $dir/pics # # 4. crete a tmp dir in $dir as well, and make the permission to 757 # by using this command: chmod 757 tmp # # 4. you might want to get the rmads script as well. this script # cleans up the tmp dir. read the script for info on setting up # a cron job. # # 5. if you want a statistic file, then create another file with # the exact rc file name but with the extension .db, and change # the permission to 646. it must be empty file. # touch stat.db; chmod 646 stat.db # # With the capability to set cookie on cookieable browsers, it is more # reliable to direct to appropriate url. # # Due to some servers do not support cookie header, a "Location" http # header cannot be used. As a result, I had to open the image file # and print the data to STDOUT. If your server supports cookie header, # then you are welcome to comment out the output img part, and add # the Location tag: # print "Location: http://$URL\n\n"; # Remember to delete one "\n" from the "content" header! # ############################################################################### # the base url to the banner images, this is the url where all the banner pics # located. uncomment this out if your server can support cookie. also, # you need to go to the bottom 10 lines to uncomment the print statements. # For some # reason, my server doesn't allow setting cookie and the Location to be set # together. # #$IMGURL = "www.ittc.ukans.edu/~skang/banner/pics"; # dir to where the banner info located. contains the stat file, pics dir, # tmp/ip files, etc. # $dir = "/home/skang/public_html/banner"; $default = "www.granax.com/~skang/freenshare.html"; # if your server has more than one domain names, then you can specify all of # them here. this is optional. it is used to set cookie. $domain = ''; # cookie, this contains the URL of the page, this is the thing that Netscape # supports. # #($name,$cookie) = split(/=/,$ENV{'HTTP_COOKIE'}); $cookie = $ENV{'HTTP_COOKIE'}; #$cookie =~ s/([^URL]*)URL=([^;]*);.*/$2/; $cookie =~ /URL=([^;]*)/; $cookie = $1; # get the remote IP so that in case the ip file is not created, we can still # kind of guess the link to go to. # #$ip = $ENV{'REMOTE_ADDR'} ? "tmp/$ENV{'REMOTE_ADDR'}" : "tmp/129.237.125.68"; $ip = "tmp/$ENV{'REMOTE_ADDR'}"; # get the action from the env var. two actions are allowed, # 'url' - points to the "sponsor" page. # 'img' - returns the img to be displayed. # ($dum,$action,$file) = split(/\//,$ENV{'PATH_INFO'}); #$action =~ s/\///; #$action = "url"; # this file contains the information about the banner to be displayed # it has the format -> url%%img%%frequency%%showed_frequency # ** currently, the showed_freq is not implemented yet. # $file = "stat" unless $file; ###################### # ACTION! ###################### # redirect url #### # if ($action eq 'url') { if ( -e "$dir/$file.db" && open(F,"+<$dir/$file.db") ) { my($raw,$hits,$url); my(@raws,@hits,@urls); my $gotit = 0; while () { chop; ($raw,$hits,$url) = split(/\t/,$_); if ($url eq $cookie) { $hits++; $gotit=1; } push @urls,$url; push @raws,$raw; push @hits,$hits; } seek(F,0,0); for $raw (@raws) { $hits = shift @hits; $url = shift @urls; print F "$raw $hits $url\n"; } print F "1 1 $cookie\n" unless $gotit; } # there is cookie. if ( $cookie ) { #print "Content-type: text/html\n\n"; print "Location: http://$cookie\n\n"; #print "

$ENV{HTTP_COOKIE}"; } # redirect URL #### non cookie #### #################################### # browser does not support cookie, we need to open the tmp file # created to redirect to new URL. elsif ( (-e "$dir/$ip") && $action eq 'url' ) { open(TMP,"$dir/$ip"); $URL = ; close(TMP); unlink("$dir/$ip"); print "Location: http://$URL\n\n"; } else { print "Location: http://$default\n\n"; } # # generate a random img and url to go to, and set cookie no matter the browser # supports cookie or not. if there is no HTTP_COOKIE defined, then create a # file so that the action 'url' will be able to get the url. } elsif ($action eq 'img') { open(F,"$dir/$file") || die "Error opening file $dir/$file: $!\n"; while (chop($line = )) { ($url,$img,$freq,$sfreq) = split(/%%/,$line); push(@imgs,"$img"); push(@urls,"$url"); #push(@freqs,$freq); #push(@sfreqs,$sfreq); for ($i=0; $i<$freq; $i++) { push(@imgs,"$img"); push(@urls,"$url"); } } close(F); # pick a random seed to randomly display banner srand(time|$$); $random = int(rand(scalar(@imgs))); $cookie = $urls[$random]; $pic = "$imgs[$random]"; # record hits if ( -e "$dir/$file.db" && open(F,"+<$dir/$file.db") ) { my($raw,$hits,$url); my(@raws,@hits,@urls); my $gotit = 0; while () { chop ; ($raw,$hits,$url) = split(/\t/,$_); if ($url eq $cookie) { $raw++; $gotit=1; } push @urls,$url; push @raws,$raw; push @hits,$hits; } seek(F,0,0); for $raw (@raws) { $hits = shift @hits; $url = shift @urls; print F "$raw $hits $url\n"; } print F "1 0 $cookie\n" unless $gotit; } # if there is no cookie, then we can write the URL, if there # is cookie # then we don't write to file since it is not necessary. # write the URL so that when user clicks on the link, will # bring to the exact page! if (! $ENV{'HTTP_COOKIE'} && ! $ENV{HTTP_USER_AGENT} =~ /Mozilla/ ) { open(TMP,">$dir/$ip") || die "Error opening file $dir/$ip: $!\n"; print TMP $cookie; print TMP "\n$ENV{HTTP_USER_AGENT}"; close(TMP); } # set cookie if (defined $domain) { print "Set-Cookie: URL=$cookie; path=/; $domain\n"; } else { print "Set-Cookie: URL=$cookie; path=/\n"; } #print "Pragma: no-cache\n"; print "Content-type: image/gif\n\n"; # output image open(IMG,"$dir/pics/$pic"); #while ( $img=) { print $img; } print ; close(IMG); # if your server supports cookies, then uncomment the two print # statements. and comment the above three lines. # also, you need to comment this too # print "Content-type: image/gif\n\n"; # # make sure that the browser displays the new image, # Netscape version 2.*(1.*) do not do that. #print "Pragma: no-cache\n"; #print "Location: http://$IMGURL/$pic\n\n"; } else { print "Content-type: text/html\n\n"; print "Unknown action. Please check your url\n"; exit; }