Code:
#!/usr/bin/perl -w
use strict;
my($expDate) = "Wednesday, 09-Nov-03 00:00:00 GMT";
my($theDomain) = ".engr.iupui.edu";
my($path) = "/cgi-bin/";
setCookie("user", "dbewley", $expDate, $path, $theDomain);
setCookie("user_addr", $ENV{'REMOTE_HOST'}, $expDate, $path, $theDomain)
if defined($ENV{'REMOTE_HOST'});
setCookie("flag", "black", $expDate, $path, ".iupui.edu");
setCookie("car", "honda:accord:88:LXI:green", $expDate, $path, $theDomain);
my(%cookies) = getCookies();
print("Content-type: text/html\n\n");
print("<HTML>");
print("<HEAD><TITLE>The Cookie Display</TITLE></HEAD>");
print("<BODY>");
print("<H1>Cookies</H1>");
print("<TABLE BORDER=1 CELLPADDING=10>");
foreach (sort(keys(%cookies))) {
print("<TR><TD>$_</TD><TD>$cookies{$_}</TD></TR>");
}
print("</TABLE>");
print("</BODY>");
print("</HTML>");
sub setCookie {
my($name, $val, $exp, $path, $dom, $secure) = @_;
print("Set-Cookie: ");
print("$name=$val, expires=$exp, path=$path, domain=$dom");
print(", $secure") if defined($secure);
print("\n");
}
sub getCookies {
my(%cookies);
foreach (split (/; /,$ENV{'HTTP_COOKIE'})){
my($key) = split(/=/, $_);
$cookies{$key} = substr($_, index($_, "=")+1);
}
return(%cookies);
}
Here's another simple cookie script I tested on the server with no luck:
#!/usr/bin/perl -w
# cc-set.cgi - set cookie with CGI::Cookie
use CGI qw(:standard);
use CGI::Cookie; # this line is new
unless (param()) {
# display form
print
header(),
start_html("Cookie Baker"),
h1("Cookie Baker"),
start_form(),
p("What's your name?", textfield("NAME")),
submit(),
end_form(),
end_html();
} else {
# process form and set cookie
$name = param("NAME");
$to_set = CGI::Cookie->new(-name => "username",
-value => $name,
-expires => "+30s",
); # this has changed
print
header(-cookie => $to_set),
start_html("Thanks!"),
h1("Thanks for using the Cookie Baker"),
p("I set your name to ", b($name),
" and I will remember this if you visit ",
a({-href => "cc-get.cgi"}, "here"),
" within the next 30 seconds."),
end_html();
}