#!/usr/bin/perl # This line says "Hey, Man, I'm a Perl program!" # It points to the Location of the Perl Interpreter on your server. # This tells the server that your program # needs to be interpreted/executed # Some windows environments will not require this line # Note: when saving this script from your browser you will need to save it as # scipt.pl or script.cgi ######### This Section is where we will START to define all of our variables ######### # The name of our text file we are going to use $FileName = "info.txt"; # The path (not the url!) to the directory where the text file is located # For UNIX it would look like this $ServerPath = "/www/usr/web/"; # For WIN32 it would look like this $ServerPath = "C:\\www\\usr\\web\\"; $ServerPath = "/www/webref/webref/dev/perl/"; ######### This is where we will END identifying our variables ################### # This calls the Parse Form SUBROUTINE &parse_form; # This calls our date accessor functions SUBROUTINE (i.e. returns the date) &GetDate; # This Prints the appropriate HTTP Header Type so that the browser knows we are sending it text or html # if we didn't have this we would get an internal server error sub PrintHeader { print "Content-type: text/html\n\n"; } # This next section examines the Query String # The informatoin we passes to the program by appending the ? and then the command # i.e. when we called the script http://www.yourdomain.com/cgi-bin/script.cgi?exec # everything after the ? would be the Query_String-- in this case it would be "exec" # This is a standard if statement # if (this expression is true) { # Do This! # } if ($ENV{'QUERY_STRING'} =~ /exec/i) { # Now we could change the above line to the below to make it easier to understand, but # by using the "=~ /exec/i" instead of eq "exec" we are telling the program to look for # any string containing the string "exec" regardless of case....we could pass it ?ExEcdog # and it would work as opposed to the below version where we would have to pass it the literal # translation or "exec" case sensitive and with no accompanying characters. # if($ENV{'QUERY_STRING'} eq "exec") { # If the correct Query String is passed we will do whatever we are told to do between the opening # and closing brackets {} # NOTE that we do not need semicolons after the opening if statement and the closing bracket # First we call the print header subroutine to tell the browser what kind of output to expect # and to print the appropriate HTTP header &PrintHeader; # And then we print out a few lines of text using our standard print statement termintaed by a semicolon # you might want to try and add a few lines here in this format: # print "whatever text you want"; # It is important to note here that for the most part Perl is not case or # whitespace sensitive (not including variables) print "Hey, man, it worked!\n
"; print "I'm executed!\n
"; print "$date\n
"; # The "\n" you see is Perl's new line character, which is basically just like hitting the return button # in a text editor. It is not necessary but does make it easier to read the code if you were to view # source on the page produced } # Terminate true expression # If we don't pass it a Query String let's, that is just call our program, we will call the LogMe subroutine else { # beginning of else statement &LogMe # Call the LogMe subroutine } # terminate else statement # Here is another SUBROUTINE that takes care of writing our information to the text file # The format for a subroutine -a block of code which accomplishes a specific task- # sub SomeName { # put code here # } # End subroutine sub LogMe { # Open file we specified in our variables our senmd an error to our log file # The word "FILE" is called a file handle and we can substitute any name we want as long as we # keep it uniform open(FILE,">>$ServerPath$FileName") || die ("no luck with opening file $dir$file"); # Print the Environmental variables about the user to the text file separated by a | character print FILE "$shortdate|$hour:$min $ampm|$ENV{'HTTP_REFERER'}|$ENV{'REMOTE_ADDR'}|$ENV{HTTP_USER_AGENT}|$ENV{'DOCUMENT_NAME'}\n"; # Make sure we close our file, using the file handle close(FILE); # print the appropriate HTTP header &PrintHeader; # Let us know we are done print "You Have Been Logged!"; } #End LogMe subroutine sub Error { &PrintHeader; print "You Have Done Something Wrong! -Ha Ha"; } sub GetDate { @days = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday); @months = (January,February,March,April,May,June, July,August,September,October,November,December); $time = time; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time); $sam = $mon+1; $shortdate = "$mday/$sam/$year"; $ampm = "AM"; if ($hour eq 12) { $ampm = "PM"; } if ($hour eq 0) { $hour = "12"; } if ($hour > 12) { $hour = ($hour - 12); $ampm = "PM"; } if ($min < 10) { $min = "0$min"; } $year = 1900+$year; $todaydate = "$days[$wday], $mday $months[$mon] $year"; $date = $todaydate . " $hour\:$min $ampm"; } #This is the parse form subroutine #It converts the Name/Value pairs #It also facilitates both Get and Post requests sub parse_form { if ($ENV{'REQUEST_METHOD'} eq 'GET') { # Split the name-value pairs @pairs = split(/&/, $ENV{'QUERY_STRING'}); } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { # Get the input read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @pairs = split(/&/, $buffer); } else { #Print the Header so the browser knows what is coming &PrintHeader; print "Bad Request Method"; } # Get the input # read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); # Un-Webify plus signs and %-encoding $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s///g; if ($allow_html != 1) { $value =~ s/<([^>]|\n)*>//g; } else { } $FORM{$name} = $value; } }