#1/usr/local/bin/perl $file = "/users/robertm/guestbook.dat $date = localtime(time); $ENV{"REQUEST_METHOD"} eq "POST" ? &Add_Guest($file) : &Display_book($file); sub Add_Guest { local ($guestbookfile) = @_; local (%data_received, $new_guest, @guestbook); # Decode the user data and place it in the # data_received associative array. %data_received = &User_data(); &No_SSI(*data_received); if ($data_received{"name"} ne " ") { $new_guest = "Name: $data_received{\"name"\}
\n"; $new_guest .= "Date: $date
\n"; $new_guest .= "Email:$data_received{\"email\"}
\n" if $data_recieved{"email"}ne " "; $new_guest .= "Home Page URL:$data_recieved{\"url\"}
\n" if $data_recieved{"url\"} ne " "; $new_guest .= "$data_recieved{\"country\"}
\n" if $data_received{ "country"} ne " "; $new_guest .= " data_received{\"comments\"} \n" if $data_received{"comments"} ne " "; $new_guest .= "


\n"; open(GUESTBOOK, "$guestbookfile")|| die "Content-type: text/text \n\nCannot open $guestbookfile"; @guestbook = ; close(GUESTBOOK); unshift(@guestbook, $new_guest); open(GUESTBOOK,">$guestbookfile")|| die "Content-type: text/text \n\nCannot open $guestbookfile"; # Lock the guestbook file now. flock (GUESTBOOK, 2); print GUESTBOOK @guestbook; # Unlock the guestbook file now. flock (GUESTBOOK, 8); close(GUESTBOOK); &Display_book($guestbookfile); } else { print "Content-type: text/html\n\n"; print "

Sign-in Unsuccesful

\n"; print "You must enter your name to be added to the g-book."; } } sub Display_Book { local ($guestbookfile) = @_; local (@guestbook); open(GUESTBOOK, "$guestbookfile") || die "Content-type: text/text\n\nCannot open $guestbookfile"; @guestbook = ; close (GUESTBOOK); print "Content-type: text/html\n\n"; print "My Guestbook
Make your own free website on Tripod.com
"; print "

My guest book




"; print @guestbook; print "
"; } sub No_SSI { local (*data) = @_; foreach $key (sort keys(%data)) { $data{$key} =~ s///g; } } sub User_Data { local (%user_data, $user_string, $name_value_pair, @name_value_pairs, $name, $value); # If the data was sent via POST, then it is avaliable # from standard input. Otherwise, the data is in the # QUERY_STRING enviroment variable. if ($ENV{"REQUEST_METHOD"}eq "POST") { READ(STDIN,$user_string,$ENV{CONTENT_LENGTH"}); } else { $user_string = $ENV{"QUERY_STRING"}; } # This line changes the + signs to spaces. $user_string =~ s/\+/ /g; # This line places each name/value pair as a separte # element in the name_value_pairs array. @name_value_pairs = spilt(/&/, $user_string); # This code loops over each element in the name_value_pairs # array, splits it on the = sign, and places the value # into the user_data associativearray with the name as the # key. foreach $name_value_pair (@name_value_pairs) { ($name, $value) = split (/=/, $name_value_pair); # These two lines decode the values from any URL # hexadecimal encoding. The first section searches for a # hexadecimal number and the second part converts the # hex number to decimal and returns the character # equilvalent. $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/ge; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/ge; # If the name/value pair has already been given a value, # as in the case of multiple items being selected, then # separate the items with a " : ". if (defined($user_data{$name})) { $user_data{$name} .= " : ". $value; } else { $user_data{$name} = $value; } } return %user_data; }