Here are set_cookie and get_cookie subs that I have used in the past. I stole these from something, but I don't remember what. Please feel free to correct them if anything is wrong with them.
######################################################################
+#####
# Get_cookie - Gets all the cookies and returns them in a hash
# Usage: my %cookie = &get_cookie;
######################################################################
+#####
sub get_cookie {
my @cstuff = @_;
my ($cookie, $value, $char, %cookie);
my @Cdec = ('\+', '\%3A\%3A', '\%3D', '\%2C', '\%25', '\%2B', '\%26'
+,'\%3B');
my %Cdec = ('\+',' ','\%3A\%3A','::','\%3D','=','\%2C',',','\%25','%
+','\%2B','+','\%26','&','\%3B',';');
if ($ENV{'HTTP_COOKIE'}) {
foreach (split(/; /,$ENV{'HTTP_COOKIE'})) {
($cookie,$value) = split(/=/);
foreach $char (@Cdec) {
$cookie =~ s/$char/$Cdec{$char}/g;
$value =~ s/$char/$Cdec{$char}/g;
}
$cookie{$cookie} = $value;
}
}
return %cookie;
}
######################################################################
+#####
# set_cookie - Sets a cookie.
# Usage : set_cookie('Name',"Value",?) ? = 0 or 1. 0 = temp, 1 = perma
+nent
######################################################################
+#####
sub set_cookie {
my @cookie = @_;
my ($cookie, $value, $type, $char);
my @Cenc = ('\;','\&','\+','\%','\,','\=','\:\:','\s');
my %Cenc = ('\;','%3B','\&','%26','\+','%2B','\%','%25','\,','%2C','
+\=','%3D','\:\:','%3A%3A','\s','+');
my $header = '';
for (my $i = 0; $i <= $#cookie; $i = $i + 3) {
($cookie, $value, $type) = @cookie[$i .. $i+2];
foreach $char (@Cenc) {
$cookie =~ s/$char/$Cenc{$char}/g;
$value =~ s/$char/$Cenc{$char}/g;
}
$header = 'Set-Cookie: ' . $cookie . '=' . $value . ';';
if ($type == 1) {
$header .= ' expires=Friday, 31-Dec-2010 00:00:00 GMT; path=/';
}
print "$header\n";
}
}
|