CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Setting and Reading Cookies in Perl



Dec 8, 2000, 11:16 AM

Post #1 of 3 (1401 views)
Setting and Reading Cookies in Perl Can't Post

I need to set and read a cookie in perl.


User browses in, perl script checks to see if cookie exists, if it does, read it and act upon its value, if not, set a cookie.

I have no clue how to set and read cookies in perl, and have not been able to find an article any where.

Code examples welcome!

-Aaron Bockover

[This message has been edited by abockover (edited 12-08-2000).]

Enthusiast / Moderator

Dec 8, 2000, 11:44 AM

Post #2 of 3 (1401 views)
Re: Setting and Reading Cookies in Perl [In reply to] Can't Post

You could use cookie.lib which is the easiest way, no code needed, just sub routine calls, like:

require cookie.lib;
&SetCookies("user", "john doe");

It's easy, you can read about how to do it in the lib file, if you email me I can send it to you, I can't remember where I found it.



Dec 8, 2000, 1:06 PM

Post #3 of 3 (1403 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");

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;

This will store the value of any cookies in the hash %cookie.
To check the value, just use:
which will return "VALUE_OF_COOKIE".

Living life -umop apisdn-


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives