I’m having trouble setting and reading cookies with a Perl script. I was wondering if anyone has a “generic” perl script that will help me accomplish my goal.
Thanks
I’m having trouble setting and reading cookies with a Perl script. I was wondering if anyone has a “generic” perl script that will help me accomplish my goal.
Thanks
Here’s what I use. To set a cookie, call the SetCookie sub right after you print the “Content type” line, before the blank line followed by HTML (IOW, in the header).
use strict;
my $Cookie_Exp_Date = ‘’;
my $Cookie_Path = ‘’;
my $Cookie_Domain = ‘’;
my $Secure_Cookie = ‘0’;
my @Cookie_Encode_Chars = qw/% + ; , = & :: \s/;
my %Cookie_Encode_Chars = (’%’, ‘%25’,
‘+’, ‘%2B’,
‘;’, ‘%3B’,
‘,’, ‘%2C’,
‘=’, ‘%3D’,
‘&’, ‘%26’,
‘::’, ‘%3A%3A’,
‘\s’, ‘+’);
my @Cookie_Decode_Chars = qw/+ %3A%3A %26 %3D %2C %3B %2B %25/;
my %Cookie_Decode_Chars = (’+’, ’ ',
‘%3A%3A’, ‘::’,
‘%26’, ‘&’,
‘%3D’, ‘=’,
‘%2C’, ‘,’,
‘%3B’, ‘;’,
‘%2B’, ‘+’,
‘%25’, ‘%’);
my @Weekday = qw/Sun Mon Tue Wed Thu Fri Sat/;
my @MonthName = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
sub GetCookies
{
my (%Cookies,$cookie,$value,$char);
if ($ENV{‘HTTP_COOKIE’})
{
foreach (split(/; /,$ENV{‘HTTP_COOKIE’}))
{
($cookie,$value) = split(/=/);
foreach $char (@Cookie_Decode_Chars)
{
$cookie =~ s/$char/$Cookie_Decode_Chars{$char}/g;
$value =~ s/$char/$Cookie_Decode_Chars{$char}/g;
}
$Cookies{$cookie} = $value;
}
}
return %Cookies;
}
sub GetCookieDate
{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,isdst) = gmtime(_[0]);
$wday = $Weekday[$wday]; # convert number to a string.
$mon = $MonthName[$mon];
if ($hour<10) { $hour = “0” . $hour }
if ($min<10) { $min=“0” . $min }
if ($sec<10) { $sec = “0” . $sec}
if ($mday<10) { $mday = “0” . $mday }
$year = $year + 1900;
return( $wday . “, " . $mday . “-” . $mon . “-” . $year . " " . $hour . “:” . $min . “:” . $sec . " GMT” );
}
sub SetCookie {
my ($cookie,$value,$exp,$path,$domain) = @_;
my $char;
foreach $char (@Cookie_Encode_Chars)
{
$cookie =~ s/$char/$Cookie_Encode_Chars{$char}/g;
$value =~ s/$char/$Cookie_Encode_Chars{$char}/g;
}
print 'Set-Cookie: ’ . $cookie . ‘=’ . $value . ‘;’;
if ($exp)
{
$exp = GetCookieDate($exp);
print ' expires=' . $exp . ';';
}
if ($path) { print ' path=' . $path . ';' }
if ($domain) { print ' domain=' . $domain . ';' };
print "
Have you tried using the CGI.pm? It makes manageing cookies very easy. You just create cookies using the cookie() function with a -name and -value parameters and placing it in the header. then to retireve the cookie all you need is the name. An example:
#/usr/bi/perl
use CGI;
$mycookie = cookie(-name=>‘sdmbname’, -value=>‘Tretiak’);
print header(-cookie=>$mycookie);
##Read the cookie
$name = cookie(‘sdmbname’);
print(‘This cookie code brought to you by $name’);
There are some other things you can do with it, but that is the basics.
I second the reccomendation for using CGI.pm. It makes things much easier. In addition, CGI.pm will take any incoming cookies and stick them in a scalar of the same name without you having to do anything. Example (this is off the top of my head, so it might have errors.)
my $query = new CGI;
if (!($query->param('foo'))) { # no cookie
my $cookie = $query->cookie(-name=>'foo', -value=>'Hello!');
print $query->header(-cookie=>$cookie);
print "You did not have a cookie, so I gave you one!";
} else {
print $query->header;
print "You already have a cookie. The value is: ", $query->param('foo');
}