you've got the HTTP_COOKIE environment variable set (as long as you also add an expiry date) to read from. Your example works because it takes the first cookie off the variable which I guess works because you've just created it so it's tagged on to the front.
Just to clarify, these are not exactly true. :-) when you set a cookie - doesn't matter where you set it, from a server side script or from the browser - it is set in the browser itself. It's the browser that
sends that cookie information to the server. This is why you can't "read" it until the second page view after setting it.
As for the expiration date - there are two basic types of cookies, a
session cookie and a
persistent cookie. A session cookie is set, but since it has no valid expiration date, remains until the browser is closed. The above works not because of any tagging, but because it's set and the very next page view reads it. But yes - because it's the last cookie set, it's at the "top of the list." This is why you may or may not see other data added to "Wilma;...."
page 1: set cookie
page 2: browser SENDS the cookie value to the server and we can read it.
A persistent cookie is one with a valid expiration date and when you close the browser, it "lives" until the expiration date. This is a pretty big difference - when you set a persistent cookie in FireFox, you can go to Options and actually see the cookie, which I don't think you can with session cookies - they are still there though.
Like I said that was just for the JS/Perl puzzle, in actual use you'd always want to loop through cookies to access all keys and values, like
$cdata = $ENV{'HTTP_COOKIE'};
@pairs = split(/;/,$cdata);
foreach $pair (@pairs) {
($name,$value) = split(/=/,$pair);
print "Cookie name is $name, value is $value<br>";
}
There's more to it in practical applications, like cleansing data, etc., but that's the gist.
oops: was thinking PHP there for a sec