
Maebius
Novice
Dec 8, 2000, 1:06 PM
Post #3 of 3
(539 views)
|
|
Re: Setting and Reading Cookies in Perl
[In reply to]
|
Can't Post
|
|
Well. if you don't want to use any libs, here's what I did in one of my scripts. My example only sets a basic cookie that expires when the user closes their browser. It sets the cookie named "EverRPGPlayer" with a value of info from a form. (see form-parsing topics elsewhere) Might not be the best way, but... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> #somewhere in your code... # &SetCookie ("EverRPGplayer", $fromweb{"PlayerLogin"}, ); # rest of code # sub SetCookie { #SetCookies uses the following format for passing info. # name, value, expiration, path, # domain, secure(1 or 0) local($name, $value, $expire, $path, $domain, $secure) =@_; print "Set-Cookie: "; print ($name, "=", $value, , $expire, $path, $domain, $secure, "\n"); } </pre><HR></BLOCKQUOTE> Then, if you want to read in the cookie in another script, or lelsewhere in the same script... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> # Read in cookie names and values # @nvpairs=split(/; /, $ENV{'HTTP_COOKIE'}); foreach $pair (@nvpairs) { ($name, $value) = split(/=/, $pair); $cookie{$name} = $value; } </pre><HR></BLOCKQUOTE> This will store the value of any cookies in the hash %cookie. To check the value, just use: $cookie{$NAME_OF_COOKIE} which will return "VALUE_OF_COOKIE". ------------------ Maebius Living life -umop apisdn- maebius@everthorn.net
|