#!/usr/bin/perl # Michael Tartaglia # 10 April 2002 # Making & Reading Cookies use CGI ":standard"; require template; # REDIRECT BACK TO COOKIE ENTRY FORM IF THERE IS NO QUERY print redirect ("../cookie.shtml") if ($ENV{QUERY_STRING} eq ""); my $name = clean(param("name")); # READ USER's NAME my $from = clean(param("from")); # READ HOME CITY my $food = clean(param("food")); # READ FAVORITE FOOD my $site = $template::baseUrl; my $exp = "+30d"; # EXPIRATION OF COODIE $name = "anonymous" if ($name eq ""); # SET DEFAULTS... $food = "food" if ($food eq ""); $from = "homeland" if ($from eq ""); # WRITE COOKIES print "Set-cookie: Name=$name; expires=$exp; path=\n"; print "Set-cookie: From=$from; expires=$exp; path=\n"; print "Set-cookie: Food=$food; expires=$exp; path=\n"; print "Set-cookie: SITE=$site; expires=$exp; path=\n"; # PRINT OUT THAT EVERYTHING PROCEEDED AS PLANNED template::startHTML("Assignment 11.1","Making and Setting Cookies"); print "
A cookie has been saved on your computer which sould ", "expire shortly. So, ", "$name, you may now go have some ", "$food in your native ", "$from. Have a nice day!", "

", "Read what was exactly saved, or", "
Change the cookie's contents.", "
"; template::endHTML(); sub clean # SUBROUTINE REGULATES WHITESPACES AND ='s IN STRING { my $str = $_[0]; $str =~ s/^\s*|=//g; $str =~ s/;\s*/ /g; return $str; }