Hi,
I'm using a .cgi script written in Perl on a Linux/Apache server to set a cookie.
This is the first time I've ever played around with setting cookies, and I'm having some, but not complete success.
Here's one of my Set-Cookie statements:
$session_id = "4918b9fe1f73d715b494589fe0c38d28";
$action = "main";
print "Set-Cookie:ID=$session_id; action=$action; path=/; expires=Thu, 31-Dec-2003 00:00:00 GMT\n";
Here's the resulting cookie that gets saved to a temporary file on my computer:
ID
4918b9fe1f73d715b494589fe0c38d28
my.domain.com/
1536
143147008
29609777
2033440848
29543362
*
Note that in the above example, ID=$session_id successfully went into the cookie, but that action=$action did not get set in the cookie!!!
Just for kicks, I reversed the order of "ID" and "action" in the set cookie statement and the opposite thing happened: "action" got set and "ID" did not!! (see below)
print "Set-Cookie:action=$action; ID=$session_id; path=/; expires=Thu, 31-Dec-2003 00:00:00 GMT\n";
Here's the resulting cookie that gets saved to a temporary file on my computer:
action
main
my.domain.com/
1536
143147008
29609777
2718318144
29543361
*
In both cases, "path" and "expires" do get set.
Anybody seen anything like this before or have any ideas?
Thanks...






critical posted this at 20:53 — 3rd February 2003.
They have: 46 posts
Joined: May 2002
I think I solved my own problem.
I think I need a separate Set_Cookie statement for each key-value pair. I'm still testing but it looks like that's what it is. Duh.
print "Set-Cookie:ID=$session_id; path=/; expires=Thu, 31-Dec-2003 00:00:00 GMT\n";
print "Set-Cookie:action=$action; path=/; expires=Thu, 31-Dec-2003 00:00:00 GMT\n";
critical posted this at 21:20 — 3rd February 2003.
They have: 46 posts
Joined: May 2002
Nope - that's not working either... When I read the cookie, it's only grabbing the first of the 2 sets... I still need your help...
# GET COOKIE CRUMBS
my %crumbs = "";
if ($ENV{HTTP_COOKIE} ne "") {
my @cookies = split(/;/,$ENV{HTTP_COOKIE});
foreach my $cookie (@cookies) {
my ($name, $value) = split(/=/,$cookie);
$crumbs{$name} = $value;
} # end for
} # end if
&Error(" 1 $crumbs{ID} 2 $crumbs{action}");
Result:
1 9b9df633f4dbb23c5bca988167031ac4
2
Wil posted this at 22:55 — 3rd February 2003.
They have: 601 posts
Joined: Nov 2001
OK. Start off. Please don't roll your own Cookie parser/set routine. Please consider using CGI.pm or any of the Cookie::, CGI::Cookie, modules on the CPAN.
Here's the relevent bit in the CGI.pm docs about cookies.
http://stein.cshl.org/WWW/software/CGI/#cookies
- wil
critical posted this at 14:14 — 4th February 2003.
They have: 46 posts
Joined: May 2002
Thanks for the advice about using an existing module. I'm sure that's the best approach. Unfortunately I don't use CGI.pm and would prefer not to download and install any other modules.
Can anyone help me out with a do-it-yourself reinvent-the-wheel read-more-than-one-cookie code in Perl?
I can set multiple cookies. And my code (above) can successfully read the first one. But it's not reading in additional cookies after the first.
TIA.
Wil posted this at 14:47 — 4th February 2003.
They have: 601 posts
Joined: Nov 2001
CGI.pm comes with Perl. You don't need to download anything.
Reading cookies is an absolute walk in the park, too. Try something like:
my $cookie1 = $query->cookie('cookie1);my $cookie2 = $query->cookie('cookie2');
...
'
Can't get any easier than that.
- wil
Wil posted this at 14:50 — 4th February 2003.
They have: 601 posts
Joined: Nov 2001
Let me give you a few more examples.
use CGI;my $query = CGI->new();
sub set_cookie {
my $cookie = $query->cookie (
-name => 'mycookie',
-value => 'value',
-expires => '+1M',
-path => '/',
-domain => 'mydomain.com'
);
print $query->header( -cookie=> $cookie );
}
sub retrieve_cookie {
my $cookie = $query->cookie('mycookie');
}
'
Hope that helps.
- wil
critical posted this at 15:00 — 4th February 2003.
They have: 46 posts
Joined: May 2002
Thank you for the examples - I really appreciate them. I do recognize that CGI.pm is available to me and that it is supposed to make many tasks easier and more efficient, but I'm an old dog, and that's a new trick that I'm going to continue to resist using for a while - too many other things to learn...
I did, however, figure this out. The book I got the cookie parsing routine out of had a typo.
Here's the content of $ENV{HTTP_COOKIE}:
ID=16bed70d0bc216db0572bce1bf48dcb9; action=main
So I needed to add a space after the semicolon in my split statement...
---from---
my @cookies = split(/;/,$ENV{HTTP_COOKIE});
---to---
my @cookies = split(/; /,$ENV{HTTP_COOKIE});
I can now read multiple cookies. Thanks again for the help.
ROB posted this at 17:22 — 4th February 2003.
They have: 447 posts
Joined: Oct 1999
if you don't want to use cgi.pm search for "cookie-lib.pl" -- i used it along time ago but i don't recall having any problems. Even if you don't use it the code will help you with your own implementation.
Wil posted this at 09:39 — 5th February 2003.
They have: 601 posts
Joined: Nov 2001
cookie-lib has been replaced with CGI.pm as of Perl 5.x. Please resist using any of the *-lib modules, including cgi-lib.pl. They're outdated and insecure.
- wil